可以使用Python的内置模块json来打开和解析JSON文件,并将其转换为字典。以下是一个示例:

import json

# 打开JSON文件
with open('data.json') as file:
    # 读取文件内容
    data = file.read()

    # 解析JSON数据
    json_data = json.loads(data)

    # 将JSON数据转换为字典
    dictionary = dict(json_data)

# 打印字典
print(dictionary)

在上面的示例中,我们首先使用open函数打开JSON文件,并使用read方法读取文件内容。然后,我们使用json.loads函数将JSON数据解析为Python对象。最后,我们使用dict函数将Python对象转换为字典。

请确保将data.json替换为你要打开的实际JSON文件的路径。

python打开json文件并转成字典

原文地址: https://www.cveoy.top/t/topic/iL5I 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录