[Tutor] Reading information from a text file into a list of lists (WinXP/py2.6.2)

Alan Gauld alan.gauld at btinternet.com
Mon Oct 26 02:02:47 CET 2009

"Katt" <the_only_katala at verizon.net> wrote

> def read_important_dates():
>    print "\nReading text file into program: important.txt"
>    text_file = open("important.txt", "r")
>    dates = text_file.readlines()
>    text_file.close()
>    print dates
> #
> read_important_dates()
> When it gets to print dates I see the following:
> [ ' "Old Test","2009_10_20"\n',  ' "Current Test",2009_10_25"\n',  ' 
> "Future Test","2009_11_01" ' ]
> Does it look this way because I am using the "print"?  Or do I still need 
> to add the inner [ ] and strip out the \n and '?

Its that way because you are just storing the raw strings read from the 
(notice the outer single quotes). You need to interpret(ie parse) the 
to extract the data. You should also strip the lines to get rid of the \n 

> If I add brackets to the text file I am reading from my output looks like

If you have control over the text file then add a unique character as
a field separator and then uyou can use split() to split the fields easily.
Common choices of field separator include unlikely currency symbols
or graphics characters.

> I have tried line.strip() to remove the \n and ' characters, but get the 
> following error:
>    File "fileio.py", line 6, in read_important_dates
>        line.strip()
> NameError: global name 'line' is not defined

And sure enough you have no line variable. You will need to
process the lines one by one. A list comp can do this for you

>    dates = text_file.readlines()

dates = [line.strip() for line in text_file]

And if you knew the sepsatator char you could do that too

dates = [line.strip().split(sep) for line in text_file]

>    File "fileio.py", line 6, in read_important_dates
>        important_dates = dates.split()
> NameError: 'list' object has no attribute 'split'

True ebough. You really need to process the lines one at a time.
This is usually best done as you read them from the file rather
than after storing them in a list - it saves processing the data twice.


Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list