FAQ: How do I calculate what quoted strings and numbers mean?

p.lavarre at ieee.org p.lavarre at ieee.org
Tue Nov 21 00:03:59 CET 2006


> > This idiom makes sense of ordinary Python literals (such as 010, 0x8,
> > 8.125e+0, and "\x45ight") ...
> ...
> sure, but the os module isn't very far away:

So now I think this is true:

"""
FAQ: How should I calculate what numbers and quoted strings mean?

A:

If you know the type of result you want, then you can choose a
corresponding evaluator, such as:

int('0x100', 0x10)
float('1.23e+4')
binascii.unhexlify('4142')
repr('\xA3')[1:-1].decode('string_escape')
repr(u'\u00A3')[2:-1].decode('unicode_escape')

If you need to allow any of a few types of input, then you can call
these evaluators in order of priority, and catch their exceptions.

If you need to allow any type of literal input, then you can resort to
the "limited" shlex or the "slow and dangerous" eval, or call all the
evaluators, or roll your own lexxer.
"""




More information about the Python-list mailing list