Populating a list

A. Keyton Weissinger keyton at weissinger.org
Sun Dec 9 20:01:48 EST 2001


So we're clear. You can either do it this way:

states = []
f=open('/home/mlorfeld/states.txt', 'r+').readlines()
for i in f:
  states.append(i.rstrip())
print states

The rstrip() method, as you can probably guess, strips white spaces from the
RIGHT end of the string.

or (from Mr. Martelli -- I like this one better):

f = open('/home/mlorfeld/states.txt', 'r+')
states = [ line[:-1] for line in f.readlines() ]
f.close()

Sorry for the misinformation, folks.

Keyton





More information about the Python-list mailing list