我可以获取文件中的所有对象。这是我遇到的错误:
with open("Resultfill", "rb") as Resultfillread:
FileNotFoundError: [Errno 2] No such file or directory: 'Resultfill'
这意味着该文件不存在。由于下面的答案,这个问题已经解决。
但我总是无法访问文件中包含的对象,特别是使用yield. 这是文件二进制文件“testfile”中包含的对象
di = {"c1": 3, "c2": 5, "c3": 13, "c4": 12, 5: 8, 6: 8, 7: 1, 8: 5, 9: 12, 10: 2, 11: 20, 12: 16, 13: 45, 14: 6, 15: 4 }
di = {1: 3, 2: 5, 3: 13, 4: 12, 5: 8, 6: 8, 7: 1, 8: 5, 9: 12, 10: 2, 11: 20, 12: 16, 13: 45, 14: 6, 15: 4 }
这是代码:
os.chdir("C:/Program Files/Sublime Text 3/testpython")
open("testfile", "ab")
def loadobject(fillname):
with open(filename, "rb") as test:
while True:
try:
yield pickle.load(test)
except EOFError:
break
genaratetest = loadobject("testfile")
print(Resultfillwrite)
for testobject in genaratetest:
print(testobject)
我希望 print(testobject) 每次循环打印一个对象,在本例中,第一个字典“di”,然后是第二个。
萧十郎
相关分类