繁体   English   中英

如何使用python从大文本文件中搜索dict

[英]How to search for dict from a big text file using python

我有巨大的文本文件,我必须解析。

文件的每一行包含一些文本和字典。 我只关心字典数据。

文件包含以下格式的日志

my data : {"a":1, "b":2, "c": 3}
my data : {"a":23, "b": 44, "c": 565}
my_data : {"a":1233, "b": 21, "c":544}

因此,根据以上数据,我仅查找dict。

我尝试过

f = open(‘text.file’,'r’)
my_dict = eval(f.read())

但是它给了我错误,因为该行的起始部分是字符串。 因此,我的问题是从文件中提取字典的最佳方法是什么。

看起来您在字符串之间有一些修饰符,因此str.split()是您的朋友。

之后,考虑使用AST模块而不是评估板。 与盲目评估相比,它带来的安全风险要少。

>>>import ast
>>> a = ast.literal_eval("{'a':1}")
>>> type(a)
<class 'dict'>
>>> a
{'a': 1}

评估不好

这是我会做的:

import json

dicts = []
with open('text.file', 'r') as f:
    for line in f.readlines():
        if not line: continue
        _, dict_str = line.split(':', 1)
        dict_str = dict_str.strip()
        dict = json.load(dict_str)
        dicts.append(dict)

您可以使用re模块

import re
text = """my data : {"a":1, "b":2, "c": 3}
          my data : {"a":23, "b": 44, "c": 565}
          my_data : {"a":1233, "b": 21, "c":544}"""
dict = re.compile(r"{[^}]*?}", re.I)
matches = dict.finditer(text)
for match in matches:
    my_dict = eval(match.group())
    print(my_dict) 

这给你

{'b': 2, 'c': 3, 'a': 1}
{'b': 44, 'c': 565, 'a': 23}
{'b': 21, 'c': 544, 'a': 1233}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM