Python 解析 JSON 文件并生成新文件:详细指南
假设有一个名为'input.json'的文件,内容如下:
{
"name": "John",
"age": 30,
"city": "New York"
}
以下是一个示例代码,用于解析并生成新的 JSON 文件:
import json
# 打开输入文件
with open('input.json') as f:
data = json.load(f)
# 修改数据
data['age'] = 35
data['city'] = 'San Francisco'
# 打开并写入输出文件
with open('output.json', 'w') as f:
json.dump(data, f)
运行以上代码后,会生成一个名为'output.json'的新文件,内容如下:
{
"name": "John",
"age": 35,
"city": "San Francisco"
}
原文地址: https://www.cveoy.top/t/topic/oevB 著作权归作者所有。请勿转载和采集!