python json读取文件转换为字典
要将一个 JSON 文件读取并转换为字典,可以使用 Python 的 json 模块。下面是一个简单的示例:
import json
# 读取 JSON 文件
with open('data.json') as file:
data = json.load(file)
# 打印字典
print(data)
在上面的示例中,假设有一个名为 data.json 的 JSON 文件,它的内容如下:
{
"name": "John",
"age": 30,
"city": "New York"
}
运行上面的代码将输出以下结果:
{'name': 'John', 'age': 30, 'city': 'New York'}
这样,JSON 文件中的数据就被读取并转换为一个 Python 字典了。
原文地址: https://www.cveoy.top/t/topic/i7gt 著作权归作者所有。请勿转载和采集!