1 回答

TA貢獻(xiàn)1772條經(jīng)驗 獲得超8個贊
使用json模塊將字符串加載到列表中。遍歷列表中的字典并設(shè)置一個屬性 -calc為lat值減去50。
最后,如果需要,將列表轉(zhuǎn)儲到帶有可選indentarg的字符串中,以進(jìn)行漂亮的打印。
import json
s = '''[{
"lat": 43.96063343238712,
"panoid": "sffcNG69c2kdZwEuYp1htw",
"lon": 3.098330084924494
}, {
"lat": 43.96052745649745,
"panoid": "2rJPv_r0gC5FBPLZK5vHDA",
"lon": 3.098487422195691
}]'''
l = json.loads(s)
for d in l:
d['calc'] = d['lat'] - 50
print(json.dumps(l, indent=4))
給予:
[
{
"lat": 43.96063343238712,
"panoid": "sffcNG69c2kdZwEuYp1htw",
"lon": 3.098330084924494,
"calc": -6.0393665676128805
},
{
"lat": 43.96052745649745,
"panoid": "2rJPv_r0gC5FBPLZK5vHDA",
"lon": 3.098487422195691,
"calc": -6.039472543502548
}
]
添加回答
舉報