我有一个包含多个 JSON 行的文件,如下所示。
{"status str":null,"id":563221, "filter":"low","text" : "Grass is green"}
{"status str":null,"id":612835, "filter":"high","text" : "Textual blue"}
我想要的输出应该只显示 ID 号和“草是绿色的”作为 [key : value] 对,如 Python 中的字典:
563221:“草是绿色的”
612835:“文字蓝色”
我目前正在使用 ObjectPath 进行查询。使用元组,我可以输出所有数据,但我不能选择数据的部分。下面是我正在使用的代码。
read_data = []
with open(fileName, 'r') as file_to_read:
for line in filetoread:
json_tree = objectpath.Tree(read_data)
dict = {tuple(json_tree.execute('$.id')) : tuple(json_tree.execute('$.text'))}
line = next(filetoread)
return dict