简体   繁体   English

Python TypeError:整数很奇怪

[英]Python TypeError: an integer is required weird

wally=pickle.load(open("The Wall.txt","rb"))
usernamey=pickle.load(open("username1.txt","rb"))
wu=("\n", wally, "\n", usernamey, "\n")
apost=input("Enter your post below: \n")
pwickle=pickle.dump(wu, apost, open("The Wall.txt","wb"))

Don't know why this isn't working, comes up with error message: 不知道为什么这不起作用,出现错误消息:

pwickle=pickle.dump(wu, apost, open("The Wall.txt","wb"))

TypeError: an integer is required

As unutbu pointed out, the third argument is the protocol. 正如unutbu指出的那样,第三个参数是协议。 You should group all the objects you want to pickle in a tuple like this: 您应该将所有要腌制的对象归为一个元组,如下所示:

pwickle=pickle.dump((wu, apost), open("The Wall.txt","wb"))

If you need to pickle a larger amount of data, you could use the shelve module that operates like a dictionary. 如果需要腌制大量数据,则可以使用像字典一样工作的shelve模块。

By the way, the files pickle creates are binary (or at least not human readable, depends on the protocol), so I wouldn't name them .txt . 顺便说一句,pickle创建的文件是二进制文件(或者至少不是人类可读的,取决于协议),所以我不会将它们命名为.txt .db , .pickle or something like that would be better. .db.pickle或类似的东西会更好。

第三个参数指定协议,协议应为整数,0、1或2。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM