Python使用pandas库读取txt文件中的Json数据,并导出到csv文件
使用的是 Python 3
代码:
#!/usr/bin/python # -*- coding: gbk -*- # JSON数据导出到csv文件 import json import pandas file = open("data//001.txt", "r", encoding="utf-8") data = file.read() file.close() jsonData = json.loads(data) i = 0 dataList = [] for hitsItem in jsonData['hits']['hits']: i = i + 1 if i <= 10000000: source = hitsItem['_source'] item = {'plate_no': source['plate_no'], 'tollgate_name3': source['tollgate_name3'], 'pass_time': source['pass_time']} dataList.append(item) print("数据总数:" + str(len(dataList))) df = pandas.DataFrame(dataList, columns=['plate_no', 'tollgate_name3', 'pass_time']) df.to_csv('data//001.csv', index=False, header=True, encoding="utf-8-sig") print("完成")