Pickle: TypeError:需要一个类似bytes的对象,而不是'str'[复制]

时间:2022-06-08 18:20:45

This question already has an answer here:

这个问题已经有了答案:

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

当我在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?

我尝试通过encode函数将fname1转换为字节,但它仍然没有解决问题。有人能告诉我怎么了吗?

4 个解决方案

#1


16  

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:

顺便说一句,你应该使用with来处理打开/关闭文件:

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


6  

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

在Python 3中,您需要特别地调用‘rb’或‘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


1  

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

您需要将“str”更改为“bytes”。试试这个:

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


1  

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.

PickleDB有问题,需要修复。

Line 201 of pickledb.py

pickledb.py的第201行

From:

来自:

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

to:

:

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

Problem solved forever.

问题解决了,直到永远。

#1


16  

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:

顺便说一句,你应该使用with来处理打开/关闭文件:

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


6  

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

在Python 3中,您需要特别地调用‘rb’或‘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


1  

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

您需要将“str”更改为“bytes”。试试这个:

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


1  

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.

PickleDB有问题,需要修复。

Line 201 of pickledb.py

pickledb.py的第201行

From:

来自:

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

to:

:

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

Problem solved forever.

问题解决了,直到永远。