How to store the contents of a file into a list?
Daniel
Daniel.Kinnaer at AdValvas.be
Sat Jan 6 16:27:51 EST 2001
On Sat, 6 Jan 2001 14:44:45 -0600 (CST), Skip Montanaro
<skip at mojam.com> wrote:
>Try this:
>
> f = open("/tmp/recordfile")
> l1 = []
> for line in f.readlines():
> line = line.rstrip() # remove trailing line ending
> l1.append(line.split(",")
>
>The split at the commas maps the string "rec11,rec12,rec13" into the list
>["rec11","rec12","rec13"]. That list gets appended to l1 which winds up as
>a list of lists of strings instead of a list of strings.
>
>Cheers,
>
>--
>Skip Montanaro (skip at mojam.com)
Skip, Emile, thanks for the replies. I've learned what that split()
means after all!
best regards,
Daniel
>Support the Mojam.com Affiliates Program: http://www.mojam.com/affl/
>(847)971-7098
>
More information about the Python-list
mailing list