使用python从JSON文件中提取部分数据 [英] Extract part of data from JSON file with python
问题描述
我一直试图从JSON文件中仅提取某些数据.我设法将JSON解码并将所需的数据放入python dict中.当我打印出字典时,它会显示所有想要的数据,但是当我尝试将字典写入新文件时,只会写入最后一个对象. 我不明白的一件事也是为什么当我打印字典时会得到多个字典对象而不是我期望的1.
I have been trying to extract only certain data from a JSON file. I managed to decode the JSON and get the wanted data into a python dict. When I print out the dict it shows all the wanted data, but when I try to write the dict into a new file, only the last object gets written. One thing that I can't understand is also why when I print the dict I get multiple dicts objects instead of 1 as I would expect.
我的代码:
import json
input_file=open('json.json', 'r')
output_file=open('test.json', 'w')
json_decode=json.load(input_file)
for item in json_decode:
my_dict={}
my_dict['title']=item.get('labels').get('en').get('value')
my_dict['description']=item.get('descriptions').get('en').get('value')
my_dict['id']=item.get('id')
print my_dict
back_json=json.dumps(my_dict, output_file)
output_file.write(back_json)
output_file.close()
我的json.json文件:
my json.json file:
[
{"type":"item","labels":{"en":{"language":"en","value":"George Washington"}},"descriptions":{"en":{"language":"en","value":"American politician, 1st president of the United States (in office from 1789 to 1797)"}},"id":"Q23"},
{"type":"item","aliases":{"en":[{"language":"en","value":"Douglas Noël Adams"},{"language":"en","value":"Douglas Noel Adams"}]},"labels":{"en":{"language":"en","value":"Douglas Adams"}},"descriptions":{"en":{"language":"en","value":"English writer and humorist"}},"id":"Q42"},
{"type":"item","aliases":{"en":[{"language":"en","value":"George Bush"},{"language":"en","value":"George Walker Bush"}]},"labels":{"en":{"language":"en","value":"George W. Bush"}},"descriptions":{"en":{"language":"en","value":"American politician, 43rd president of the United States from 2001 to 2009"}},"id":"Q207"},
{"type":"item","aliases":{"en":[{"language":"en","value":"Velázquez"},{"language":"en","value":"Diego Rodríguez de Silva y Velázquez"}]},"labels":{"en":{"language":"en","value":"Diego Velázquez"}},"descriptions":{"en":{"language":"en","value":"Spanish painter who was the leading artist in the court of King Philip IV"}},"id":"Q297"},
{"type":"item","labels":{"en":{"language":"en","value":"Eduardo Frei Ruiz-Tagle"}},"descriptions":{"en":{"language":"en","value":"Chilean politician and former President"}},"id":"Q326"}
]
打印my_dict输出:
print my_dict output:
{'id': u'Q23', 'description': u'American politician, 1st president of the United States (in office from 1789 to 1797)', 'title': u'George Washington'}
{'id': u'Q42', 'description': u'English writer and humorist', 'title': u'Douglas Adams'}
{'id': u'Q207', 'description': u'American politician, 43rd president of the United States from 2001 to 2009', 'title': u'George W. Bush'}
{'id': u'Q297', 'description': u'Spanish painter who was the leading artist in the court of King Philip IV', 'title': u'Diego Vel\xe1zquez'}
{'id': u'Q326', 'description': u'Chilean politician and former President', 'title': u'Eduardo Frei Ruiz-Tagle'}
在文件test.json中输出:
output in the file test.json:
{"id": "Q326", "description": "Chilean politician and former President", "title": "Eduardo Frei Ruiz-Tagle"}
我也想知道为什么字典会输出'title':u'Diego Vel \ xe1zquez' 但是,如果我去打印my_dict.values()[2],我会得到通常写为DiegoVelázquez的名字.
Also I would like to know why the dict is outputing 'title': u'Diego Vel\xe1zquez' but if i go print my_dict.values()[2] i Get the name written normaly as Diego Velázquez.
非常感谢
推荐答案
您的代码会为每个对象创建新的字典对象,
Your code creates new dictionary object for each object with:
my_dict={}
此外,它会覆盖变量的先前内容.从内存中删除 m_dict 中的 Old 词典.
Moreover, it overwrites the previous contents of the variable. Old dictionary in m_dict is deleted from memory.
尝试在for循环之前创建一个列表,并将结果存储在该列表中.
Try to create a list before your for loop and store the result there.
result = []
for item in json_decode:
my_dict={}
my_dict['title']=item.get('labels').get('en').get('value')
my_dict['description']=item.get('descriptions').get('en').get('value')
my_dict['id']=item.get('id')
print my_dict
result.append(my_dict)
最后,将结果写入输出:
Finally, write the result to the output:
back_json=json.dumps(result, output_file)
打印字典对象旨在通过显示数据类型来帮助开发人员.在u'Diego Vel \ xe1zquez'中,开头的 u 表示Unicode对象(字符串).打印使用对象时,将根据操作系统中当前的语言设置对其进行解码.
Printing the dictionary object aims to help the developer by showing the type of the data. In u'Diego Vel\xe1zquez', u at the start indicates a Unicode object (string). When object using is printed, it is decoded according to current language settings in your OS.
这篇关于使用python从JSON文件中提取部分数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!