[Tutor] Reading List from File
Brett Wilkins
lupin at orcon.net.nz
Thu Jul 31 16:06:44 CEST 2008
This is what I'd use...
But it'd also be rather easy with regex...
Oh well.
Here:
>>> f = open('intext', 'r')
>>> foo = f.readline().strip().replace('"','').split(',')
>>> foo
['aaa', 'bbb', 'ccc']
Cheers
On 1/08/2008, at 1:49 AM, amingv at gmail.com wrote:
> If your list is in the format:
> aaa,bbb,ccc
>
> You can use
> foo = in_file.readline.split(',')
>
> --
> Amin Rainmaker
> From: "S Python" <spython01 at gmail.com>
> Date: 1 August 2008 1:07:22 AM
> To: tutor at python.org
> Subject: [Tutor] Reading List from File
>
>
> Hi Everyone,
>
> I am trying to read a comma-delimitted list ("aaa","bbb","ccc") from
> a text file and assign those values to a list, x, such that:
>
> x = ["aaa", "bbb", "ccc"]
>
> The code that I have come up with looks like this:
>
> >>> x = []
> >>> f = open(r'c:\test.txt', 'r')
> >>> x.extend(f.readlines())
> >>> x
> ['"aaa","bbb","ccc"']
>
> If you look closely, there is an extra pair of single quotes (')
> that encapsulates the string. Therefore, len(x) returns 1, instead
> of 3. Is there a function to "separate" this list out? I hope my
> question makes sense.
>
> Thanks in advance.
>
> Samir
>
>
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20080801/df19e007/attachment.htm>
More information about the Tutor
mailing list