1 回答
TA贡献1951条经验 获得超3个赞
我这样做的一般方法是在日志文件中查找结构,然后将其提取出来。:查看您共享的数据,感兴趣的行上有一个字符和十六个逗号分隔值。由于数据不是直接放入json的形式,所以我存储在一个临时字典中,然后将其转换为json字符串。示例如下:
import json
# parse the log file and store in dictionary
raw_data = {}
with open('unstructured_data.txt') as log:
for line in log:
line = line.rstrip()
if line.count(':') == 1:
heading, data = line.split(':')
fields = data.split(',')
if len(fields) > 15:
raw_data[heading.lstrip()] = fields
# Put only data of interest in to another python dictionary
result_data = {}
for i in range(len(raw_data['core id'])):
result_data[raw_data['core id'][i].strip()] = raw_data['pass/fail'][i].strip()
# Convert python dictionary to json string
result_json = json.dumps(result_data)
print(result_json)
从您的日志文件中给出以下内容:
$ python3 parse_log.py
{"0": "pass", "1": "pass", "2": "pass", "3": "pass", "4": "pass", "5": "pass", "6": "pass", "7": "pass", "64": "pass", "65": "pass", "66": "pass", "67": "pass", "68": "pass", "69": "pass", "70": "pass", "71": "pass", "": ""}
虽然这不是一个完美的结果,但它应该有望通过实际数据得到改进。
添加回答
举报