使用 python 从 JSON 文件中提取部分数据
Extract part of data from JSON file with python
我一直在尝试从 JSON 文件中仅提取某些数据。我设法解码 JSON 并将想要的数据放入 python 字典中。当我打印出字典时,它显示了所有需要的数据,但是当我尝试将字典写入新文件时,只写入了最后一个对象。
我无法理解的一件事也是为什么当我打印字典时我得到多个字典对象而不是我期望的 1 个对象。
我的代码:
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文件:
[
{"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输出:
{'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:
{"id": "Q326", "description": "Chilean politician and former President", "title": "Eduardo Frei Ruiz-Tagle"}
我也想知道为什么字典输出 'title': u'Diego Vel\xe1zquez'
但是如果我去打印 my_dict.values()[2] 我得到的名字通常写成 Diego Velázquez。
非常感谢
当你这样做时:
for item in json_decode:
您正在遍历文件中的每一行。
每次循环都会覆盖 my_dict 变量,这就是为什么输出中只有一行的原因。
加载文件后,您可以简单地打印出 json_decode
变量来执行您想要的操作。
您的代码为每个对象创建新的字典对象:
my_dict={}
此外,它覆盖了变量之前的内容。 m_dict 中的旧 词典已从内存中删除。
尝试在 for 循环之前创建一个列表并将结果存储在那里。
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)
最后将结果写入输出:
back_json=json.dumps(result)
打印字典对象旨在通过显示数据类型来帮助开发人员。在u'Diego Vel\xe1zquez'中,开头的u表示一个Unicode对象(字符串)。打印 object using 时,它会根据 OS.
中的当前语言设置进行解码
我一直在尝试从 JSON 文件中仅提取某些数据。我设法解码 JSON 并将想要的数据放入 python 字典中。当我打印出字典时,它显示了所有需要的数据,但是当我尝试将字典写入新文件时,只写入了最后一个对象。 我无法理解的一件事也是为什么当我打印字典时我得到多个字典对象而不是我期望的 1 个对象。
我的代码:
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文件:
[
{"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输出:
{'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:
{"id": "Q326", "description": "Chilean politician and former President", "title": "Eduardo Frei Ruiz-Tagle"}
我也想知道为什么字典输出 'title': u'Diego Vel\xe1zquez' 但是如果我去打印 my_dict.values()[2] 我得到的名字通常写成 Diego Velázquez。
非常感谢
当你这样做时:
for item in json_decode:
您正在遍历文件中的每一行。
每次循环都会覆盖 my_dict 变量,这就是为什么输出中只有一行的原因。
加载文件后,您可以简单地打印出 json_decode
变量来执行您想要的操作。
您的代码为每个对象创建新的字典对象:
my_dict={}
此外,它覆盖了变量之前的内容。 m_dict 中的旧 词典已从内存中删除。
尝试在 for 循环之前创建一个列表并将结果存储在那里。
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)
最后将结果写入输出:
back_json=json.dumps(result)
打印字典对象旨在通过显示数据类型来帮助开发人员。在u'Diego Vel\xe1zquez'中,开头的u表示一个Unicode对象(字符串)。打印 object using 时,它会根据 OS.
中的当前语言设置进行解码