如何基于密钥分离大文件并使用python将文件存储在单独的词典中?

时间:2017-02-23 00:03:05

标签: python python-3.x python-2.7 file

成为python的新手我试图弄清楚如何将以下格式的示例文件分成多个部分,并根据与键“type”关联的值将其存储在单独的字典中。可以任何人共享一些输入就此而言。感谢。

[
{"type": "A", "verb": "NEW", "key": "96f55c7d8f42", "event_time": "2017-01-06:12:46:46.384Z", "last_name": "Smith", "adr_city": "Middletown", "adr_state": "AK"},
{"type": "B", "verb": "NEW", "key": "ac05e815502f", "event_time": "2017-01-06:12:45:52.041Z", "customer_id": "96f55c7d8f42", "tags": {"some key": "some value"}},
{"type": "C", "verb": "UPLOAD", "key": "d8ede43b1d9f", "event_time": "2017-01-06:12:47:12.344Z", "customer_id": "96f55c7d8f42", "camera_make": "Canon", "camera_model": "EOS 80D"},
{"type": "D", "verb": "NEW", "key": "68d84e5d1a43", "event_time": "2017-01-06:12:55:55.555Z", "customer_id": "96f55c7d8f42", "total_amount": "12.34 USD"}
]
~    

2 个答案:

答案 0 :(得分:0)

假设您的文件是data.txt,请尝试以下操作:

import json

with open('data.txt') as data_file:
    data = json.load(data_file)

for row in data:
    print("Type: " + row['type'])
    print(row)
    print()

答案 1 :(得分:0)

最简单的方法可能是使用字典词典。

编写一个读取每一行的迭代器,并使用ujson将文本解析为dict。然后pop要用作键的值,并返回两个部分。

 import ujson

 def read_lines(file_path):
     with open(file_path, 'r') as fh:
         for line in fh:
             dictionary = ujson.loads(line)
             key = dictionary.pop('key')
             yield key, dictionary


dictionaries = dict()
for key, value in read_lines(r"/foo/bar/file.txt"):
    dictionaries[key] = value