By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,996 Members | 1,343 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,996 IT Pros & Developers. It's quick & easy.

String from File -> List without parsing

P: n/a
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?

--
Greg
Sep 4 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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 Horvath wrote: 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?

--
Greg

Sep 4 '05 #2

P: n/a
Gregor Horvath wrote:
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:
def munch(astrg): .... 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
Sep 4 '05 #3

P: n/a
John Machin wrote:
f = file("I050901.ids").readlines()
Y3K bug alert :-)


but then there is Python 3000 and Hurd, which solves all problems of
this universe :-)
Something like this:
>>> def munch(astrg):
... 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?

--
Gregor
Sep 5 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.