File formats

Thread Solved
Reply

Join Date: Oct 2008
Posts: 13
Reputation: Devlan is an unknown quantity at this point 
Solved Threads: 0
Devlan Devlan is offline Offline
Newbie Poster

File formats

 
0
  #1
Nov 18th, 2008
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?
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,009
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 244
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: File formats

 
0
  #2
Nov 18th, 2008
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)
  1. >>> d1 = {'a':2, 'b':3}
  2. >>> d2 = eval(repr(d1))
  3. >>> d2
  4. {'a': 2, 'b': 3}
  5. >>> d2['c']=1
  6. >>> d1
  7. {'a': 2, 'b': 3}
  8. >>> d2
  9. {'a': 2, 'c': 1, '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.
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 977
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 273
woooee woooee is offline Offline
Posting Shark

Re: File formats

 
0
  #3
Nov 19th, 2008
But now I want to be able to save it in file, preferably in form like this:

number;name
It's a simple combination so a common text file will work just fine. There is also the advantage of being able to verify what you have written to the file via any text processor. Note that dictionaries hash keys so they are not necessarily in key order.
  1. def read_from_file():
  2. after_d = {}
  3. fp = open("test.txt", "r")
  4. for rec in fp:
  5. rec=rec.strip()
  6. key, name = rec.split(";")
  7. after_d[int(key)]=name ## convert key back to an integer
  8. fp.close()
  9. return after_d
  10.  
  11.  
  12. def save_to_file(save_d):
  13. print "before saving to file", save_d
  14. fp = open("test.txt", "w")
  15. for key in save_d.keys():
  16. fp.write("%d;%s\n" % (key, save_d[key])) ## writes everything as a string
  17. fp.close()
  18.  
  19.  
  20. before_d = {}
  21. before_d[1]="Name 1"
  22. before_d[2]="Name 2"
  23. before_d[3]="Name 3"
  24. before_d[10]="Name 10"
  25. before_d[20]="Name 20"
  26.  
  27. save_to_file(before_d)
  28. after = read_from_file()
  29. print "read from file ", after
Last edited by woooee; Nov 19th, 2008 at 1:09 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 13
Reputation: Devlan is an unknown quantity at this point 
Solved Threads: 0
Devlan Devlan is offline Offline
Newbie Poster

Re: File formats

 
0
  #4
Nov 19th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,858
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 867
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: File formats

 
0
  #5
Nov 19th, 2008
You really should use module pickle for that ...
  1. # use module pickle to save/dump and load a dictionary object
  2. # or just about any other intact object
  3.  
  4. import pickle
  5.  
  6. # create the test dictionary
  7. before_d = {}
  8. before_d[1]="Name 1"
  9. before_d[2]="Name 2"
  10. before_d[3]="Name 3"
  11.  
  12. # pickle dump the dictionary
  13. fout = open("dict1.dat", "w")
  14. pickle.dump(before_d, fout)
  15. fout.close()
  16.  
  17. # pickle load the dictionary
  18. fin = open("dict1.dat", "r")
  19. after_d = pickle.load(fin)
  20. fin.close()
  21.  
  22. print before_d # {1: 'Name 1', 2: 'Name 2', 3: 'Name 3'}
  23. print after_d # {1: 'Name 1', 2: 'Name 2', 3: 'Name 3'}
Last edited by vegaseat; Nov 19th, 2008 at 11:32 am.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC