How can I convert JSON to CSV?

前端 未结 26 1665
余生分开走
余生分开走 2020-11-21 22:32

I have a JSON file I want to convert to a CSV file. How can I do this with Python?

I tried:

import json
import c         


        
26条回答
  •  面向向阳花
    2020-11-21 23:04

    You can use this code to convert a json file to csv file After reading the file, I am converting the object to pandas dataframe and then saving this to a CSV file

    import os
    import pandas as pd
    import json
    import numpy as np
    
    data = []
    os.chdir('D:\\Your_directory\\folder')
    with open('file_name.json', encoding="utf8") as data_file:    
         for line in data_file:
            data.append(json.loads(line))
    
    dataframe = pd.DataFrame(data)        
    ## Saving the dataframe to a csv file
    dataframe.to_csv("filename.csv", encoding='utf-8',index= False)
    

提交回复
热议问题