我通过一堆URL循环到中等大小的json文件,我正试图将这些文件合并到一个文件中。目前,它将每个文件的json数据附加到一个列表中,然后我可以将其写入一个json文件
data = []
for i in range(50):
item = tree.getroot()[i][0]
with urllib.request.urlopen(url + item.text) as f:
for line in f:
data.append(json.loads(line))
# Save to file
with open('data.json', 'w') as outfile:
json.dump(data, outfile)
但是,我不确定这种方法的可伸缩性如何,我最终将不得不以这种方式组合100个文件。如果数据列表变得太大,我想尝试一次性写入它会导致我的系统由于内存问题崩溃吗?有没有一种方法可以连续写入json文件,所以不要像这样附加在循环中:
data.append(json.loads(line))
相反,我会使用以下内容写入每个循环中的文件:
with open('data.json', 'w') as outfile:
json.writedata, outfile)
这样我就可以一边构建文件,一边清除每个循环之间的内存了?你知道吗
目前没有回答
相关问题 更多 >
编程相关推荐