![](/img/trans.png)
[英]Pickle.dump(obj) dumps two lists that conatain exactly the same values but dump file has a diffrent size
[英]pickle.dump dumps nothing when appending to file
用户可能会给出一堆 url 作为命令行参数。 过去给出的所有 URL 都是用 pickle 序列化的。 该脚本检查所有给定的 URL,如果它们是唯一的,则将它们序列化并附加到文件中。 至少这是应该发生的事情。 没有附加任何内容。 但是,当我以写入模式打开文件时,会写入新的唯一 URL。 那么什么给呢? 代码是:
def get_new_urls():
if(len(urls.URLs) != 0): # check if empty
with open(urlFile, 'rb') as f:
try:
cereal = pickle.load(f)
print(cereal)
toDump = []
for arg in urls.URLs:
if (arg in cereal):
print("Duplicate URL {0} given, ignoring it.".format(arg))
else:
toDump.append(arg)
except Exception as e:
print("Holy bleep something went wrong: {0}".format(e))
return(toDump)
urlsToDump = get_new_urls()
print(urlsToDump)
# TODO: append new URLs
if(urlsToDump):
with open(urlFile, 'ab') as f:
pickle.dump(urlsToDump, f)
# TODO check HTML of each page against the serialized copy
with open(urlFile, 'rb') as f:
try:
cereal = pickle.load(f)
print(cereal)
except EOFError: # your URL file is empty, bruh
pass
Pickle 以一种特殊的格式写出你给它的数据,例如它会写一些标题/元数据/等,到你给它的文件中。
它不打算以这种方式工作; 连接两个泡菜文件并没有真正意义。 要实现数据的串联,您需要首先将文件中的任何内容读入urlsToDump
,然后使用任何新数据更新urlsToDump
,最后再次将其转储出来(覆盖整个文件,而不是追加)。
后
with open(urlFile, 'rb') as f:
你需要一个while
循环,从文件中反复解开(反复读取)直到达到EOF。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.