String from File -> List without parsing
Gregor Horvath
g.horvath at gmx.at
Mon Sep 5 00:49:29 EDT 2005
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
More information about the Python-list
mailing list