[英]How to change value in a dict?
I have so many dict data with different formats and I want to change the key place to None.我有很多不同格式的 dict 数据,我想将关键位置更改为无。 I create this function to read all value, but I can change it
我创建了这个 function 来读取所有值,但我可以更改它
def test(T, data):
if T:
T.pop(0)
for cle, valeur in data.items():
if isinstance(valeur, dict):
T.append(valeur)
elif isinstance(valeur, list):
for idx, obj in enumerate(valeur):
if isinstance(obj, dict):
T.append(obj)
else:
print(cle, valeur)
if T:
test(T, T[0])
test(T=[], data=datas)
example data:示例数据:
datas={
"first_name": "test",
"last_name": "test",
"cars": [
{"mark": "test", "type": "12"},
{"mark": "test2", "type": "7"},
],
"date_created": "2022-05-07",
"invoice_info": {
"price": 1233,
"currency": "EUR",
"total": {"product1": 12, "product2": 22},
"date": [
{"date_1": "2022-05-07", "info": {"comment": "test", "place": "France"}},
{"date_2": "2022-06-12", "info": {"comment": None, "place": "France"}},
]
}
} }
You can do你可以做
key = 'yourkey'
dic[key] = new_value
Basically you just tell python that the value for the associated key is now new_value
.基本上,您只需告诉 python 关联键的值现在是
new_value
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.