files and reading from them

Jim Dennis jimd at vega.starshine.org
Mon Mar 25 02:27:40 EST 2002


In article <a7mgsp$gvh$1 at news3.bu.edu>, ricky wrote:

> not sure what you mean
> but check out the "shelve" module

>"Avi Homes" <Moo at houston.rr.com> wrote in message
>news:auzn8.71680$Vl.2635455 at typhoon.austin.rr.com...
>> I am writing a dictionary to a file, like so
>> f.write(str(Info))

>> and what it is writing to the file is the following..
>> {'IPrice': '3', 'IName': 'lame lame', 'IDesc': 'stuff}

>> and if i write in two things, i get..
>> {'IPrice': '3', 'IName': 'lame lame', 'IDesc': 'stuff}
>> {'IPrice': '2', 'IName': 'notworking, 'IDesc': 'annoying'}
>> What i would like to do is pull a readline or somesuch.. and have it 
>> take in the whole thing, and then just let me assign that to a empty 
>> name, and turn it into a new dictionary.. like say
>>  file.seek(1)
>> //thats how i'd go to the first place in the file correct?
>> X=file.readline()

>> and now i can say print X['IName'] ... and its turned into a 
>> dictionary..  i am most likely going about this *all* wrong.

 It sounds like you're trying to write your file out in
 "repr" form (representing an object as the string or command
 that would instantiate it).

 I guess you'd do something like: 

	f=open("...","r")
	objlist = []
 	for each in f.readlines():
		objlist.append(eval(each))

 To create an list of anonymous objects.  In your case that would be
 a list of dictionaries.
 



More information about the Python-list mailing list