Populating a list

Tim Daneliuk tundra at tundraware.com
Tue Dec 11 05:50:03 CET 2001


Tim Daneliuk wrote:
> 
> mlorfeld wrote:
> >
> > I am new to python (about 72 hours into learning), and am having
> > problems with reading a file (sate abbreviations) with one item per
> > line and populating each line into a list.  The problem is that each
> > item ends with \012 after each state ie WI\012.
> >
> > Here is the code that I am using:
> >
> > f=open('/home/mlorfeld/states.txt', 'r+')
> > states=f.readlines()#populates a list from file
> > f.close
> > print states
> 
> I think the following works portably so you do not have to fiddle w/ EOL
> conventions:
> 
> f=open("xxx.txt")
> x=f.read().splitlines()
> f.close()
> 
> x is now populated with the content of the line regardless of how it ends
> (or at least this works on a FreeBSD system using a WinDoze text file).
> 

Whoops, this does NOT achieve the desired effect on Win32.  The second
line should instead be:

x=f.read().split("\n")

------------------------------------------------------------------------------
Tim Daneliuk
tundra at tundraware.com



More information about the Python-list mailing list