Home > Back-end >  Converte json file to csv file with proper formatted rows and columns in excel
Converte json file to csv file with proper formatted rows and columns in excel

Time:07-01

Currently I'm working a script that can convert json file to csv format my script is working but I need to modify it to have proper data format like having rows and columns when the json file is converted to csv file, May I know what I need to add or modify on my script?

import pandas as pd

df = pd.read_json (r'/home/admin/myfile.json')
df.to_csv (r'/home/admin/xml/myfileSample.csv', index = None, sep=":")
 

CodePudding user response:

Taking reference from your code,you can try

df.to_csv(r'/home/admin/xml/myfileSample.csv', encoding='utf-8', header=header,index = None, sep=":")

CodePudding user response:

This could be useful.

import pandas as pd

df_json=pd.read_json("input_file.json")
df_json.head()
df_json.to_csv("output_file.csv",index=False)

CodePudding user response:

Your code is all fine, Just change the to_csv to to_excel function and it should work all fine!

import pandas as pd

df = pd.read_json (r'/home/admin/myfile.json')
df.to_excel (r'/home/admin/xml/myfileSample.csv', index = None, sep=":")

Learn more about the to_excel function of pandas here:

enter image description here

  • Related