1 Replies - 231 Views - Last Post: 12 February 2013 - 12:46 AM Rate Topic: -----

#1 5mattmatt1  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 36
  • Joined: 21-July 12

Pickling Error

Posted 11 February 2013 - 08:55 PM

I am trying to make a simple pickling program but i can't figure out the error. Can someone please enlighten me.
I have checked this thing over and over, i am running Python 3.3.0 so i think that it is a conversion error because i am using a 2.7.0 tutorial but i can't find the correct conversion.
import pickle

inventory = ['Bow','Sword','Shield','Hammer']
file = open('save.txt', 'w')
pickle.dump(inventory, file)
file.close()



Is This A Good Question/Topic? 0
  • +

Replies To: Pickling Error

#2 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 116
  • View blog
  • Posts: 212
  • Joined: 11-January 13

Re: Pickling Error

Posted 12 February 2013 - 12:46 AM

You need to open the file in "write binary" mode not in "write" mode.
open('save.txt', 'wb')
Also it would probably be considered best practice to use a with/as instead of the way you have done it.
import pickle

inventory = ['Bow','Sword','Shield','Hammer']
with open('save.txt', 'wb') as myfile:
    pickle.dump(inventory,myfile)

Then you can access your data as follows:
with open('save.txt', 'rb') as myfile:
    inventory = pickle.load(myfile)
    print(inventory)


-Mek
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1