List and array code?

Sean Ross sross at connectmail.carleton.ca
Sat Aug 23 22:16:54 EDT 2003


"derek / nul" <abuseonly at sgrail.org> wrote in message
news:ss6gkvo87qn5eu279gsct60c1id8fmb6hb at 4ax.com...
> Is there any list or array code that can load lines from a file?
>
> ie what I would like is to open a file and load line by line into a list
or
> array.
>

Hi.
There are several ways to do this:

# list comprehension
lines = [line for line in file("your/file/name/here")]

# file_object.readlines()
lines = file("your/file/name/here").readlines()

# build list from iterator
lines = list(file("your/file/name/here"))

# for loop
lines = []
for line in file("your/file/name/here"):
        lines.append(line)


HTH
Sean






More information about the Python-list mailing list