.json中的Python编辑/重命名键名 [英] Python Edit/Rename Key Names in .json
本文介绍了.json中的Python编辑/重命名键名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以更改密钥名称?
我需要像示例中一样更改名称"Class123"
.我可以更改值,但不知道如何更改键名.
Is there a way to change the Key name?
I need to change the name "Class123"
like it is in the Example. I can change the Value but I don't know how to change the key name.
示例.json:
{
"Class123": "classvalue",
"name1": {
"name2": {
"name3": {
"Y": 158.8,
"X": 201.46
},
"name4": {
"Y": 159.68,
"X": 200.32
}
}
}
}
从这样开始:
with open('my.json') as json1:
data = json.load(json1)
for item in data:
推荐答案
无法更改"键名.
最好的办法是使用pop
将值复制到另一个键:
There is no way to "change" a key name.
The best you can do is to copy the value to another key by using pop
:
d = {'old_name': 1}
d['new_name'] = d.pop('old_name')
print(d)
# {'new_name': 1}
这篇关于.json中的Python编辑/重命名键名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文