Converting a string to a tuple

Fredrik Lundh fredrik at pythonware.com
Tue Apr 13 10:19:18 EDT 1999


Bruce Huzyk wrote:
> I will now take this opportunity to revise my original post.
> I wish to convert a string to a tuple.
> My sample string should have been:
> s = '(1, "abc\\tef", 2)' 
> instead of:
> s = '(1, "abc\\def", 2)' 

if that's Python source code, your string
actually contains:

    (1, "abc\tef", 2)

since \\ is Python's string representation
for a single backslash (that is, \\ in the
source code becomes \ in the actual
string).

and \t is an alias for \011 (a tab).

try printing it (using print) to see what
I mean.

but I doubt that this is the real problem --
if you have these strings in Python source
code, you could as well use your editor
to remove the quotes, right?

so if you get the data from a file or any
other external source, the plain eval
solutions work as they should.  for
simple data types like this,

    v == eval(repr(v))

is always true.

</F>





More information about the Python-list mailing list