[Tutor] Menu data from file

Alan Gauld alan.gauld at btinternet.com
Mon Apr 5 00:54:40 CEST 2010

>I can not loop over each list item like:
>                    for eachMenuData in self.menuData():
>because when I read external txt file:
>                    f=open(listFile,'r')
>                    lines=f.readlines()
>>                    f.close()
>I get the list of file lines instead of list of external list (tuple)

Thats right you will need to parse the data to convert it into the format 
you want.
This is one reason you might find it easier to use XML for storing the data
and use a tool like ElementCTree to parse it.

Alternatively you could make your data file a Python module and import it

import menudata

for eachMenuData in menudata.menuData:

Or another option is to store the data as a tuple in a pickle or shelve 
But they you need to create it from Python initially.

My personal favourite for this kind of application is the module approach.
Just edit the menu module when you want to modify the menu structure
and the import will see it as Python data.


Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list