Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python从JSON文件中提取部分数据_Python_Json_Dictionary_Extract_Decode - Fatal编程技术网

使用python从JSON文件中提取部分数据

使用python从JSON文件中提取部分数据,python,json,dictionary,extract,decode,Python,Json,Dictionary,Extract,Decode,我一直试图从JSON文件中只提取某些数据。我设法解码了JSON并将想要的数据写入python dict。当我打印出dict时,它会显示所有想要的数据,但当我尝试将dict写入新文件时,只会写入最后一个对象。 我不明白的一件事是,为什么当我打印dict时,会得到多个dicts对象,而不是我所期望的1个 我的代码: import json input_file=open('json.json', 'r') output_file=open('test.json', 'w') json_decode=

我一直试图从JSON文件中只提取某些数据。我设法解码了JSON并将想要的数据写入python dict。当我打印出dict时,它会显示所有想要的数据,但当我尝试将dict写入新文件时,只会写入最后一个对象。 我不明白的一件事是,为什么当我打印dict时,会得到多个dicts对象,而不是我所期望的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"}
]
打印我的命令输出:

{'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"}
我还想知道为什么dict会输出“title”:u'Diego Vel\xe1zquez' 但是如果我去打印我的dict.values()[2]我的名字通常写为迭戈·贝拉斯克斯

非常感谢您这样做:

for item in json_decode:
您正在循环文件中的每一行

每次通过循环时,您都重写my_dict变量,这就是为什么您的输出中只有一行

加载文件后,只需打印
json\u decode
变量即可


您的代码为每个对象创建新的dictionary对象,其中包含:

my_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)

打印dictionary对象的目的是通过显示数据的类型来帮助开发人员。在u'Diego Vel\xe1zquez'中,开头的u表示Unicode对象(字符串)。打印object using时,将根据操作系统中的当前语言设置对其进行解码。

u'Diego Vel\xe1zquez'是Unicode的Python表示形式,其中\xe1是字符á。关于第二个问题:如果打印dict,则会得到字符串(repr)的Python表示形式,而打印字符串则会得到“正常”代表性(str)。有关更多信息,请查看“repr”和“str”()