[Tutor] foreign lists

Lance E Sloan lsloan@umich.edu
Mon, 06 Aug 2001 09:04:17 -0400

"paul" wrote:
> is it possible to access a list from another file and save whatever =
> changes are made to the list after the original program has been run?  i =
> made a simple book database which matches book titles with certain =
> attributes each book contains.  i want to be able to create the main =
> list of books and have my program access that list.  i also want any =
> additions made to the list to be changed in that foreign list, so that =
> next time the program is run, the list will include the new addition.  =
> right now, though, i have it so that each time the program runs, it =
> starts with a blank list.  any suggestions???  if anyone wants the code, =
> i'll be happy to post that also.


Somebody asked a similar question a week or two ago.  He wanted to put
a list in a web page form that would be submitted back to a CGI
(written in Python, of course) that could then use it again.  The
difference is that in your problem, you want it written to a file,
rather than a web form.  I'll assume you know how to read and write to
a file.  If you just print a list, Python will format it the same way
you would if you put one in your program:

	>>> groceries = ['eggs', 'baked beans', 'spam']
	>>> print groceries
	['eggs', 'baked beans', 'spam']

So, you could print yout list to the file and you're all set.  When you
read the list back from the file, you will read it into a string, which
you can convert back to a list using the eval() function:

	>>> food = eval("['eggs', 'baked beans', 'spam']")
	>>> food
	['eggs', 'baked beans', 'spam']
	>>> food[0]

The last line of that example is just to prove that "food" really is a

This is pretty simplistic.  I've heard of a Python module called
"pickle" which will convert an object (a list is an object) into a
string that you can save to a file or however you like and you can
later "unpickle" it to get the original object back.  That would
probably be better.  I've never used it myself.

I would suggest that you reconsider lists as your choice of data
structure, though.  You should probably use a database instead.  Look
at the various modules that came with Python that contain "db" in their
names.  You'll probably find one that suits your needs.  Alternatively,
if you're wedded to the list idea, how about a list of dictionaries?
Each book would have it's own dictionary in the list:

	>>> books = [{'author': 'Monty', 'title': 'Pointed Sticks'},
	... {'author': 'Python', 'title': 'How to tell a witch'}]
	>>> books
	[{'author': 'Monty', 'title': 'Sharp objects'}, {'author': 'Python',
	'title': 'How To Tell a Witch'}]
	>>> books[0]['title']
	'Pointed Sticks'
	>>> books[1]['author']

Those last two lines I wrote are examples of getting the title of the
first book (the first is numbered zero) and the author of the second.

Lance E Sloan
Web Services, Univ. of Michigan: Full-service Web and database design,
development, and hosting.  Specializing in Perl & Python CGIs.
http://websvcs.itd.umich.edu/ - "Putting U on the Web"