TypeError:必须是str,而不是字节

我正在使用python3.3,并且在尝试简化字典时遇到了一个神秘的错误。

代码如下:

import os
import pickle
from pickle import *
os.chdir('c:/Python26/progfiles/')

def storvars(vdict):      
    f = open('varstor.txt','w')
    pickle.dump(vdict,f,)
    f.close()
    return

mydict = {'name':'john','gender':'male','age':'45'}
storvars(mydict)

我得到:

Traceback (most recent call last):
  File "C:/Python26/test18.py", line 31, in <module>
    storvars(mydict)
  File "C:/Python26/test18.py", line 14, in storvars
    pickle.dump(vdict,f,)
TypeError: must be str, not bytes

输出文件需要以二进制模式打开:

f = open('varstor.txt','w')

需要是:

f = open('varstor.txt','wb')
链接地址: http://www.djcxy.com/p/87209.html

上一篇: TypeError: must be str, not bytes

下一篇: TypeError: 'str' does not support the buffer interface