String from File -> List without parsing

G

Gregor Horvath

Hi,

given the dynamic nature of python I assume that there is an elegant
solution for my problem, but I did not manage to find it.

I have a file that contains for example on line:

['147', '148', '146']

when I read the file

f = file("I050901.ids").readlines()

I have a string

f[0] == "['147', '148', '146']"

How can I turn this string into a list

li == ['147', '148', '146']

without parsing?
 
?

=?ISO-8859-1?Q?Jean-Fran=E7ois_Doyon?=

Gregor,

You want to use eval():

Python 2.3.4 (#53, May 25 2004, 21:17:02) [MSC v.1200 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> eval('[3,54,5]') [3, 54, 5]
>>>

Cheers,
J.F.

Gregor said:
Hi,

given the dynamic nature of python I assume that there is an elegant
solution for my problem, but I did not manage to find it.

I have a file that contains for example on line:

['147', '148', '146']

when I read the file

f = file("I050901.ids").readlines()

I have a string

f[0] == "['147', '148', '146']"

How can I turn this string into a list

li == ['147', '148', '146']

without parsing?
 
J

John Machin

Gregor said:
Hi,

given the dynamic nature of python I assume that there is an elegant
solution for my problem, but I did not manage to find it.

I have a file that contains for example on line:
['147', '148', '146']
when I read the file
f = file("I050901.ids").readlines()

Y3K bug alert :)
I have a string
f[0] == "['147', '148', '146']"

The last (or sole) line is highly likely to be "['147', '148',
'146']\n". If there are multiple lines in the file, all but the last
will definitely have "\n" at the end.
How can I turn this string into a list
li == ['147', '148', '146']
without parsing?

Something like this:
.... return [x[1:-1] for x in astrg.rstrip("\n")[1:-1].split(", ")]
....
>>> munch("['147', '148', '146']") ['147', '148', '146']
>>> munch("['147', '148', '146']\n") ['147', '148', '146']
>>> munch("['147']\n") ['147']
>>> # Warning: various flavours of "nothing" give the same result:
>>> munch("['']\n") ['']
>>> munch("[]\n") ['']
>>> munch("\n") ['']
>>>

This assumes that the contents are no more complicated than in your example.

Some notes:

(1) You neither want nor need to use eval.

(2) What is creating files like that? If it is a Python script, consider
writing them using the csv module; that way, other software can read
them easily.

HTH,
John
 
G

Gregor Horvath

John said:
Y3K bug alert :)

but then there is Python 3000 and Hurd, which solves all problems of
this universe :)
Something like this:
... return [x[1:-1] for x in astrg.rstrip("\n")[1:-1].split(", ")]
Thanks!

(1) You neither want nor need to use eval.

I wanted to avoid parsing, but when parsing is an 1 liner, I think its
the better solution.
(2) What is creating files like that? If it is a Python script, consider
writing them using the csv module; that way, other software can read
them easily.

Its a Python script.
But the file is not intended to be used by another sotware, although it
should be readable by humans. I think the python syntax is better human
readable than csv.

After all the sense of this file is to make a list persistant quickly
and easily and human readable. Maybe pickleing is a third option?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top