【问题标题】:Is there an efficient way to search dictionaries in a very large file?有没有一种在非常大的文件中搜索字典的有效方法?
【发布时间】:2016-07-21 12:39:45
【问题描述】:

我有一个非常大 (> 10GB) 的 JSON 文件,其中包含多个字典。

现在,我有一些其他词典的列表。

假设我希望在这个文件中找到字典,它匹配列表中字典的两个键的值,并最终从这些字典中提取第三个键的值。我该怎么做呢?

目前,这是我的想法:

def parse(file):
    for l in file:
        yield eval(l)

# list_of_dicts is the list containing the dictionaries

for i in range(0,len(list_of_dicts)):
    generator_of_dicts = parse("huge_file.json")
    for j in generator_of_dicts:
        if j[key1] == list_of_dicts[i][key1] \
        and j[key2] == list_of_dicts[i][key2]:
            print j[key3]
            break

有更好的方法吗?

【问题讨论】:

  • 听起来你需要使用 real 数据库。

标签: python-2.7 dictionary


【解决方案1】:

我可能会为 python https://pypi.python.org/pypi/jq 使用 jq 绑定 这是对 JSON 进行查询的好工具。

不然python的JSON库就好了。

但是:10 Gb 很多 - 如果您没有 RAM,则需要流式传输到 jq 可能非常方便。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-04
    • 2015-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多