pandas Python中的JSON编辑

omhiaaxx  于 2023-05-27  发布在  Python
关注(0)|答案(5)|浏览(144)

我有一个这样的JSON文件:

{
   "Location":true,
   "adddress":[
      {
         "street1":" 1 58 4 16"
      },
      {
         "street2":" 3 76 57 12"
      }
      {
        "street3:":....
      }
      ...
      {
        "streetn":...
      }
   ]
}

我想把10加到street1,street2的最后一个数据上,所以输出看起来像这样如何实现:
预期输出:

{
   "Location":true,
   "adddress":[
      {
         "street1":" 1 58 4 26"
      },
      {
         "sttreet2":" 3 76 57 22"
      }
   ]
}
biswetbf

biswetbf1#

假设数据存储在变量json

json = { "Location": True, "adddress": [ { "street1": " 1 58 4 16" }, { "sttreet2": " 3 76 57 12" }, ] }

由于名称不同(street1sttreet2),因此有各种方法。
其中一个可以用于更多的情况,如下所示

for i in range(len(json['adddress'])):
    for key in json['adddress'][i]:
        json['adddress'][i][key] = json['adddress'][i][key][:-2] + str(int(json['adddress'][i][key][-2:]) + 10)

[Out]:

{'Location': True,
 'adddress': [{'street1': ' 1 58 4 26'}, {'sttreet2': ' 3 76 57 22'}]}

另一个,将是更多的手动,它将需要一个改变各自的值。例如,对于street1,它将如下所示

json['adddress'][0]['street1'] = json['adddress'][0]['street1'][:-2] + str(int(json['adddress'][0]['street1'][-2:]) + 10)

[Out]:

{'Location': True,
 'adddress': [{'street1': ' 1 58 4 26'}, {'sttreet2': ' 3 76 57 12'}]}

对于sttreet2,它将如下所示

json['adddress'][1]['sttreet2'] = json['adddress'][1]['sttreet2'][:-2] + str(int(json['adddress'][1]['sttreet2'][-2:]) + 10)

[Out]:

{'Location': True,
 'adddress': [{'street1': ' 1 58 4 16'}, {'sttreet2': ' 3 76 57 22'}]}
uyhoqukh

uyhoqukh2#

假设dct是你的字典。然后:

for d in dct['adddress']:
    k = next(iter(d)) # assume there's only one key in d
    vals = d[k].split()
    vals[-1] = str(int(vals[-1]) + 10)
    d[k] = ' '.join(vals)

我假设dct['adddress']列表中的每个字典只包含一个键。
结果:

>>> dct
{'Location': True, 'adddress': [{'street1': '1 58 4 26'}, {'sttreet2': '3 76 57 22'}]}
q43xntqr

q43xntqr3#

使用此代码:

def get(num):
    global data
    key = data["address"][num].keys()[0]
    sp = [int(i) for i in data["address"][num][key].split()]
    sp[-1] += 10
    sp = [str(i) for i in sp]
    data["address"][num][key][num][key] = " ".join(sp)
for i in range(len(data["address"])):
    get(i)

此代码返回:

{'Location': True, 'address': [{'street1': '1 58 4 26'}, {'sttreet2': '3 76 57 22'}]}
pxq42qpu

pxq42qpu4#

import json

json_data = """
{
  "Location": true,
  "adddress": [
    {
      "street1": " 1 58 4 16"
    },
    {
      "sttreet2": " 3 76 57 12"
    }
  ]
}
"""
var_add = 10
data = json.loads(json_data)
print(data["adddress"][0])
dict1 = data["adddress"][0]
for keys, value in dict1.items():
    print("Before adding: ", value) # 1 58 4 16
    lastTwo = int(value[-2:])
    # print 16
    # print(lastTwo)
    replacementStr = str(lastTwo + var_add)
    # prints 26
    # print(replacementStr)
    # replaces last two
    value = value[:-2] + replacementStr
    print("After adding: ", value) # 1 58 4 26

#输出:

{'street1': ' 1 58 4 16'}
Before adding:   1 58 4 16
After adding:   1 58 4 26
6uxekuva

6uxekuva5#

new_address = []
for street in a['adddress']:
    key = list(street.keys())[0]
    value = list(street.values())[0]

    list_value = value.split(' ')
    list_value[-1] = str(int(list_value[-1]) + 10)
    new_value = ' '.join(list_value)
    new_street = {key: new_value}
    
    new_address.append(new_street)
    
a['adddress'] = new_address

相关问题