【数据处理】JSON文件转CSV

使用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)#目标文件名
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值