Pickle: TypeError: a bytes-like object is required, not 'str' [duplicate]

不问归期 提交于 2019-12-12 09:28:53

问题


I keep on getting this error when I run the following code in python 3:

fname1 = "auth_cache_%s" % username
fname=fname1.encode(encoding='utf_8')
#fname=fname1.encode()
if os.path.isfile(fname,) and cached:
    response = pickle.load(open(fname))
else:
    response = self.heartbeat()
    f = open(fname,"w")
    pickle.dump(response, f)

Here is the error I get:

File "C:\Users\Dorien Xia\Desktop\Pokemon-Go-Bot-Working-Hack-API-master\pgoapi\pgoapi.py", line 345, in login
    response = pickle.load(open(fname))
TypeError: a bytes-like object is required, not 'str'

I tried converting the fname1 to bytes via the encode function, but It still isn't fixing the problem. Can someone tell me what's wrong?


回答1:


You need to open the file in binary mode:

file = open(fname, 'rb')
response = pickle.load(file)
file.close()

And when writing:

file = open(fname, 'wb')
pickle.dump(response, file)
file.close()

As an aside, you should use with to handle opening/closing files:

When reading:

with open(fname, 'rb') as file:
    response = pickle.load(file)

And when writing:

with open(fname, 'wb') as file:
    pickle.dump(response, file)



回答2:


In Python 3 you need to specifically call either 'rb' or 'wb'.

with open('C:\Users\Dorien Xia\Desktop\Pokemon-Go-Bot-Working-Hack-API-master\pgoapi\pgoapi.py', 'rb') as file:
    data = pickle.load(file)



回答3:


You need to change 'str' to 'bytes'. Try this:

class StrToBytes:
    def __init__(self, fileobj):
        self.fileobj = fileobj
    def read(self, size):
        return self.fileobj.read(size).encode()
    def readline(self, size=-1):
        return self.fileobj.readline(size).encode()

with open(fname, 'r') as f:
    obj = pickle.load(StrToBytes(f))



回答4:


I keep coming back to this stack overflow link, so I'm posting the real answer for the next time I come looking for it:

PickleDB is messed up and needs to be fixed.

Line 201 of pickledb.py

From:

simplejson.dump(self.db, open(self.loco, 'wb'))

to:

simplejson.dump(self.db, open(self.loco, 'wt'))

Problem solved forever.



来源:https://stackoverflow.com/questions/39146039/pickle-typeerror-a-bytes-like-object-is-required-not-str

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!