[英]multiple json to csv using pandas python
trying to convert multiple json files to 1 csv file试图将多个 json 文件转换为 1 个 csv 文件
tried 2 ways,尝试了2种方法,
first one using pandas, second using json and csv writer第一个使用 pandas,第二个使用 json 和 csv 编写器
about my json关于我的 json
keys are unordered and some keys are different in every file
code using writer使用编写器的代码
file_list=os.listdir('output')
count = 0
for file in file_list:
dict={}
file_path = "output/" + file
with open(file_path,'r') as f:
jsonData=json.load(f)
datafile=open('data.csv','a')
csv_writer = csv.writer(datafile)
if count == 0:
header = jsonData.keys()
csv_writer.writerow(header)
count += 1
csv_writer.writerow(jsonData.values())
if count == 1:
csv_writer.writerow(jsonData.values())
datafile.close()
problem问题
bcoz my data is unordered and different keys so in my csv file wrong value is coming under wrong header
code using pandas使用 pandas 的代码
for file in file_list:
dict={}
file_path = "output/" + file
with open(file_path,'r') as f:
jsonData=json.load(f)
for j in jsonData:
dict.update({j:[jsonData[j]]})
df=pd.DataFrame(dict)
df.to_csv("hello.csv")
problem问题
i dont know how to append in pandas
so this is showing only 2 rows bcoz of my last json file i guess
inside my json在我的 json 里面
Try this code:试试这个代码:
import pandas as pd
import json
import pathlib
data_path = pathlib.Path('.')
keys = ['Solutions', 'account_number', 'actual_reading_current','actual_reading_previous', 'address', 'amount_due']
dat = dict([(k, []) for k in keys])
for jfile in data_path.glob('*.json'):
with jfile.open('r') as ifile:
json_data = json.load(ifile)
for key in keys:
dat[key].append(json_data[key][0] if key in json_data else None)
result = pd.DataFrame.from_dict(dat)
result.to_csv('result.csv')
I first define a dictionary containing the columns that I want.我首先定义一个包含我想要的列的字典。 Then I read in the json files and append them as rows to the dictionary.然后我读入 json 文件和 append 它们作为字典的行。
Note, that I had to edit your json files, one was missing a ending quote and I had to replace the single quotes by double quotes.请注意,我必须编辑您的 json 文件,其中一个缺少结束引号,我必须用双引号替换单引号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.