我想看看python操作 *.json 文件的案例
时间: 2024-05-02 22:20:55 浏览: 80
以下是一个简单的Python示例,展示如何读取和写入JSON文件:
```
import json
# 读取JSON文件
with open('data.json', 'r') as f:
data = json.load(f)
# 处理JSON数据
for item in data:
print(item)
# 写入JSON文件
with open('output.json', 'w') as f:
json.dump(data, f)
```
在这个例子中,我们首先使用`open`函数打开一个`data.json`文件,并使用`json.load`函数将JSON数据加载到Python对象中。然后,我们可以对JSON数据进行处理,例如使用`for`循环打印每个项。
接着,我们使用`open`函数创建一个新的`output.json`文件,并使用`json.dump`函数将Python对象转换为JSON格式,并将其写入到文件中。
请注意,我们使用`with`语句来打开文件。这将确保文件在使用后正确关闭,以避免资源泄漏和其他问题。
阅读全文
相关推荐


















