假设我有一个名为file.json的文件,其中包含以下内容:
[
{
"name": "Shirt1",
"product_id": "001234"
},
{
"name": "Shirt2",
"product_id": "005678"
}
]
使用Python3,我想向这两个字典中添加以下内容:
"type": "external"
"reviews_allowed": false
这是我的代码:
import json
import os
with open("file.json", "r", encoding="utf8") as in_file:
INP = json.load(in_file)
DATA = []
for item in INP:
item["type"] = "external"
item["reviews_allowed"] = "false"
DATA.append(item)
with open("file2.json", "w", encoding="utf-8") as out_file:
out_file.write(json.dumps(DATA, indent=2, ensure_ascii=False))
os.rename("file2.json", "file.json")
然而,我得到了以下结果:
[
{
"name": "Shirt1",
"product_id": "001234",
"type": "external",
"reviews_allowed": "false"
},
{
"name": "Shirt2",
"product_id": "005678",
"type": "external",
"reviews_allowed": "false"
}
]
有没有办法去掉false周围的双引号? 非常感谢您的时间!
False
代替"false"
。 - cs95