I study "Python Programming for the Absolute Beginner" and enjoy it. The book is written for Python 2.7 (I think), but I used Python 3 and translating code, which was an interesting call.
I recently ran into a problem, I don’t know how to fix it. In the section labeled “Data Etching and Writing to a File” Here is an example where you run the following code:
import cPickle, shelve
print "Pickling lists." variety = ["sweet", "hot", "dill"]
shape = ["whole", "spear", "chip"]
brand = ["Claussen", "Heinz", "Vlassic"]
pickle_file = open("pickles1.dat", "w")
cPickle.dump(variety, pickle_file)
cPickle.dump(shape, pickle_file)
cPickle.dump(brand, pickle_file)
pickle_file.close()
I translated it to this (for python 3)
import pickle, shelve
print ("Pickling lists.")
variety = ["sweet", "hot", "dill"]
shape = ["whole", "spear", "chip"]
brand = ["Classen", "Heinz", "Vlassic"]
pickle_file = open("pickles1.dat", "w")
pickle.dump(variety, pickle_file)
pickle.dump(shape, pickle_file)
pickle.dump(brand, pickle_file)
pickle_file.close()
BUT, I get this error / output from IDLE:
Pickling lists.
Traceback (most recent call last):
File "/Users/hypernerdcc/Documents/pickles.py", line 11, in <module>
pickle.dump(variety, pickle_file)
File
"/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/pickle.py",
line 1345, in dump
Pickler(file, protocol, fix_imports=fix_imports).dump(obj)
TypeError: must be str, not bytes
Any ideas?