Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/365.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文件的对应值替换JSON值_Python_Json - Fatal编程技术网

用Python中另一个JSON文件的对应值替换JSON值

用Python中另一个JSON文件的对应值替换JSON值,python,json,Python,Json,我试图用另一个JSON文件中的值替换JSON值。 例如,我有一个JSON文件: {'data':[{'street':'1'},{'street':'2'},{'street':'3'}} 现在我想用这个JSON文件的对应值替换“street”值: {'data':[{'1':'Example Street 1'},{'2':'Example Street 2'},{'3':'Example Street 3'}]} 因此,结果应该是: {'data':[{'street':'Example s

我试图用另一个JSON文件中的值替换JSON值。 例如,我有一个JSON文件:

{'data':[{'street':'1'},{'street':'2'},{'street':'3'}}

现在我想用这个JSON文件的对应值替换“street”值:

{'data':[{'1':'Example Street 1'},{'2':'Example Street 2'},{'3':'Example Street 3'}]}

因此,结果应该是:

{'data':[{'street':'Example street 1'},{'street':'Example street 2'},{'street':'Example street 3'}}


我正在尝试学习python,所以我不知道如何解决这个问题,也无法在internet上找到解决方案。如果有人知道如何解决这个问题,请帮助我。

我们使用dictA数据字段(它是一个列表)进行迭代。对于列表中的每个元素,我们获取元素的值,因为该元素是一个字典,并检查它是否位于dataB“data”字段列表元素中,然后更改dataA[“street”]字段

dictA ={'data': [{'street': '1'}, {'street': '2'}, {'street': '3'}]}
dictB = {'data': [{'1': 'Example Street 1'}, {'2': 'Example Street 2'}, {'3': 'Example Street 3'}]}


for elementA in dictA['data']:
  for elementB in dictB['data']:
    if elementA['street'] in elementB:
       elementA['street'] = elementB[elementA['street']]
print(dictA)
使用zip函数,同时迭代两个列表

dictA ={'data': [{'street': '1'}, {'street': '2'}, {'street': '3'}]}
dictB = {'data': [{'1': 'Example Street 1'}, {'2': 'Example Street 2'}, {'3': 'Example Street 3'}]}

for elementA,elementB in zip(dictA['data'], dictB['data']):
    elementA['street'] =elementB[elementA['street']]
print(dictA)


首先,您需要加载第一个JSON文件的内容,然后加载第二个JSON文件的内容,找到相应的值(在本例中为“street”键),然后将新JSON转储到该文件中

在我的例子中,
first.json
包含
{“数据”:[{“街道”:“1”},{“街道”:“2”},{“街道”:“3”}}
second.json
包含
{“数据”:[{“1”:“示例街道1”},{“2”:“示例街道2”},{“3”:“示例街道3”}

我使用以下命令创建了一个具有指定值的新JSON文件:

import json

with open('first.json') as first_file:
    first_json = json.load(first_file)
    
with open('second.json') as second_file:
    second_json = json.load(second_file)    

new_data = []

for i in range(len(first_json['data'])):
    for j in range(len(second_json['data'])):
        key_one, value_one = list(first_json['data'][i].items())[0]
        key_two, value_two = list(second_json['data'][j].items())[0]

        if value_one == key_two:
            new_data.append({key_one: value_two})
        
with open('new_file.json', 'w+') as new_file:
    new_data = {'data': new_data}
    json.dump(new_data, new_file)
新的JSON文件应该包含这些值

{"data": [{"street": "Example Street 1"}, {"street": "Example Street 2"}, {"street": "Example Street 3"}]}

这很有效。谢谢是否也可以将elementA更改为dictB的另一个元素?例如:
dictB={'data':[{'1':'example Street 1',example_data:aa},{'2':'example Street 2',example_data:bb},{'3':'example Street 3',example_data:cc}}
现在将dictA的“Street”元素更改为相应的“example_data”值?您只需要更改行
elementA['Street']=elementB[elementA]['street']
到这个`
elementA['street']=elementB['example\u data']
`