【发布时间】:2016-12-01 05:58:04
【问题描述】:
我正在解析数千个作为 dict 输出的 html 文件。然后我想将它们组合为一个字典并以 json 格式保存到磁盘。
我不想在遍历文件时在内存中构建这个巨大的字典,我宁愿在进行过程中不断更新/写入文件。
所以不要这样:
data = {}
for e, fn in enumerate(os.listdir(path)):
fp = os.path.join(path, fn)
d = html_to_dict(fp)
data[e] = d
我想要这个:
with open('out_file.json', 'w') as f:
for e, fn in enumerate(os.listdir(path)):
fp = os.path.join(path, fn)
d = html_to_dict(fp)
# update the file dict
有什么想法吗?
【问题讨论】:
标签: python json file dictionary