使用Pandas库和其他集成的小工具来实现
import os, json
import pandas as pd
def get_json(file_path):
with open(file_path, 'r',encoding='utf-8') as file:
data = json.load(file)
dict_list=data.get('data').get('items')#第一层字典的key等于data的值是第二层字典,第二层字典的key等于items的值就是我需要的数据,是一个字典列表
df = pd.DataFrame.from_records(dict_list)
return df
def recursion_dir_all_file(path):#获取所有文件位置
file_list = []
for dir_path, dirs, files in os.walk(path):
for file in files:
file_path = os.path.join(dir_path, file)
if file_path.endswith('.txt'):
file_list.append(file_path)
return file_list
root_path=r'你的文件路径'
file_path=recursion_dir_all_file(root_path)
df_list=[]
for path in file_path:
try:
df=get_json(path)
except IndexError:
continue
df_list.append(df)
con_df=pd.concat(df_list,ignore_index=True)
print(con_df)
con_df.to_excel(root_path+'\标记用户.xlsx', index=False)#目标文件名