python读取json格式数据

python读取json格式数据

import json
import os
import pandas as pd


def get_json_file(self, file_path):
    # 读取json格式数据
    cur_dir = '/'.join(os.path.abspath('__file__').split('/')[:-1])
    file = open(os.path.join(cur_dir, file_path), 'r', encoding='utf-8')
    # file = open(file_path, 'r', encoding='utf-8')
    filelines = file.readlines()
    data = []
    for line in filelines:
        try:
            dic = json.loads(line)
            data.append(dic)
        except:
            print(0)
    data_df = pd.DataFrame(data)
    return data_df

简单版:

import json


data = open("./datafile.json", encoding='utf-8')
# 转换为python对象
strJson = json.load(data)

 

你可能感兴趣的:(python,python,json)