简体   繁体   English

多个 csv 文件数据到单个 json 文件

[英]multiple csv files data to single json file

I had two csv files named mortality1 and mortality2 and i want to insert these two csv files data into a single json file...when i am inserting these data, i am unable give the two files at the same time to json file.and this is my code我有两个名为 mortality1 和 mortality2 的 csv 文件,我想将这两个 csv 文件数据插入到一个 json 文件中......当我插入这些数据时,我无法同时将这两个文件提供给 json 文件。和这是我的代码

import csv
import json
import pandas as pd
from glob import glob
csvfile1 = open('C:/Users/DELL/Desktop/data/mortality1.csv', 'r')
csvfile2 = open('C:/Users/DELL/Desktop/data/mortality2.csv', 'r')
jsonfile = open('C:/Users/DELL/Desktop/data/cvstojson.json', 'w')
df = pd.read_csv(csvfile1)
df.to_json(jsonfile)

i want insert the 2 csv files data at the same time to the json file我想将2个csv文件数据同时插入到json文件中

If both of your csv data are having similar structure, then you can append the data frames to one another, and then convert it to a JSON. Like如果你的两个 csv 数据都具有相似的结构,那么你可以将 append 数据帧相互转换,然后将其转换为 JSON。喜欢

import csv
import json
import pandas as pd
from glob import glob
csvfile1 = open('C:/Users/DELL/Desktop/data/mortality1.csv', 'r')
csvfile2 = open('C:/Users/DELL/Desktop/data/mortality2.csv', 'r')
jsonfile = open('C:/Users/DELL/Desktop/data/cvstojson.json', 'w')

# Read and append both dataframes to single one
df = pd.read_csv(csvfile1).append(pd.read_csv(csvfile2))

# Create the json representation of all rows together.
df.to_json(jsonfile, orient="records")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM