python---json文件读取

接上一篇的json文件写入:写文章-CSDN创作中心,这里介绍使用json.load来实现json文件读取。

相对json文件写入,json文件读取更简单

import os

import json

import sys

def read_from_json(file):

    if os.path.exists(file):

        with open(file, 'r', encoding='utf-8') as f:

            # json.load()的返回值为字典

            dic_json = json.load(f)

            print(type(dic_json))

            print(dic_json)

if __name__ == "__main__":

    # 用于接收执行过程中传递过来的第一个参数

    json_file = sys.argv[1]

    read_from_json(json_file)

文件读取结果如下:

猜你喜欢

转载自blog.csdn.net/weixin_41172713/article/details/140734210