Home:ALL Converter>Problem in converting csv to json in python

Problem in converting csv to json in python

Ask Time:2022-01-07T17:18:42         Author:lakshman

Json Formatter

I am trying to convert csv file to json in python and i have an issue where in one column data has a comma but it is enclosed in double quotes. When considering it as a csv file, data is loading properly without any issues. But while converting to json it is failing saying "Too few arguments passed".

sample Data:

col1,col2,col3

apple,Fruit,good for health

banana,Fruit,"good for weight gain , good for calcium"

Brinjal,Vegetable,good for skin

while converting the above file to json, it is failed considering 2nd row has 4 columns.

Error statement: pandas.errors.ParserError: Too many columns specified: expected 3 and found 4

data=pd.read_csv(sampledata.csv,header=None)
data_json = json.loads(data.to_json(orient='records'))
with open(filename.json,'w',encoding='utf-8')as jsonf:
     jsonf.write(json.dumps(data_json,indent=4))

Author:lakshman,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/70619041/problem-in-converting-csv-to-json-in-python
yy