import pickle
#writing into the file
f = open("essay1.txt","ab+")
list1 = ["Aditya","Arvind","Kunal","Naman","Samantha"]
list2 = ["17","23","12","14","34"]
zipfile = zip(list1,list2)
print(zipfile)
pickle.dump(zipfile,f)
f.close()
#opening the file to read it
f = open("essay1","ab")
zipfile = pickle.load(f)
f.close()产出如下:
runfile('E:/Aditya Singh/Aditya Singh/untitled3.py', wdir='E:/Aditya Singh/Aditya Singh')
<zip object at 0x0000000008293BC8>
Traceback (most recent call last):
File "E:\Aditya Singh\Aditya Singh\untitled3.py", line 21, in <module>
zipfile = pickle.load(f)
UnsupportedOperation: read发布于 2020-07-14 07:19:18
您在试图打开文件的行中忘记了文件扩展名.txt,也忘记了以附加模式打开文件,这就是为什么返回的对象没有read或readline方法(pickle.load需要)。我还建议使用with keyword,而不是手动关闭文件。
import pickle
#writing into the file
with open("essay1.txt","ab+") as f:
list1 = ["Aditya","Arvind","Kunal","Naman","Samantha"]
list2 = ["17","23","12","14","34"]
zipfile = zip(list1,list2)
print(zipfile)
pickle.dump(zipfile,f)
#opening the file to read it
with open("essay1.txt", "rb") as f:
zipfile = pickle.load(f)
for item in zipfile:
print(item)输出:
<zip object at 0x7fa6cb30e3c0>
('Aditya', '17')
('Arvind', '23')
('Kunal', '12')
('Naman', '14')
('Samantha', '34')发布于 2020-07-14 07:11:52
你有essay1文件吗?还是短文?
这是试图打开没有扩展。
f = open("essay1","ab")所以读不懂。
发布于 2020-07-14 07:16:10
您的代码有两个问题:
下面是一个有效的版本:
import pickle
#writing into the file
f = open("essay1.txt","wb")
list1 = ["Aditya","Arvind","Kunal","Naman","Samantha"]
list2 = ["17","23","12","14","34"]
zipfile = zip(list1,list2)
print(zipfile)
pickle.dump(zipfile,f)
f.close()
#opening the file to read it
f = open("essay1.txt","rb")
zipfile = pickle.load(f)
print(zipfile)
f.close()https://stackoverflow.com/questions/62889770
复制相似问题