Thread: File formats
View Single Post
Join Date: Oct 2004
Posts: 3,862
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: 869
Moderator
vegaseat's Avatar
vegaseat vegaseat is online now Online
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