![]() |
| ||
| File formats I'm trying to get to grips with using files, particurlarly saving a set of inputs from the user into a file and then load it all back up again. I have a program which, through a little object oriented interactivity, allows the user to pair up names and numbers in a dictionary. But now I want to be able to save it in file, preferably in form like this: number;name I've read a few tutorials and about the cpickle method, but I'm simply lost on how to do something like this in real life. Anyone know of some good - thourough - reading material or even have the time to write an explanation? |
| ||
| Re: File formats pickling is more beneficial for custom classes. For a built-in class (dictionary, list, etc.) simply use eval (NOTE: this solution has been posted before on this forum, but I couldn't find it) >>> d1 = {'a':2, 'b':3}So for what you're looking for you'd write the repr() bit to a file, and then use eval after reading said file.*I think the previous solution used execfile instead of bothering to open said file; however like I said I couldn't find it and don't remember exactly, but it answered exactly what you're asking. Otherwise, use the forum search and look for "pickle" to find examples of dumping/loading pickled objects. |
| ||
| Re: File formats Quote:
def read_from_file(): |
| ||
| Re: File formats Thanks, this helps a lot. I'm ever so slowly learning to actually like working with python, and every little stupid question answered is a step in that direction. |
| ||
| Re: File formats You really should use module pickle for that ... # use module pickle to save/dump and load a dictionary object |
| All times are GMT -4. The time now is 12:04 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC