pandas对json文件的读取

def json_str(file_name: str) -> str:
    #传入文件路径,返回json文件字符串
    fr = open(file_name, 'r', encoding='utf-8')
    return fr.read()

def read_json(self, file_name):
    # 读取本地id文件
    with open(file_name, 'r', encoding='utf-8') as f:
        json_list = json.load(f)
    return json_list

json_str函数用于将json文件读取为str格式,read_json函数用于将json文件读取为list格式。

在使用pandas读取json时,需要使用json_str函数的方式

json_info = json_str(path)
df = pd.read_json(json_info)

 

posted @ 2022-11-04 16:20  毛绒绒  阅读(469)  评论(0编辑  收藏  举报