How can I convert JSON to CSV?

前端 未结 26 1635
余生分开走
余生分开走 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:08

    I know it has been a long time since this question has been asked but I thought I might add to everyone else's answer and share a blog post that I think explain the solution in a very concise way.

    Here is the link

    Open a file for writing

    employ_data = open('/tmp/EmployData.csv', 'w')
    

    Create the csv writer object

    csvwriter = csv.writer(employ_data)
    count = 0
    for emp in emp_data:
          if count == 0:
                 header = emp.keys()
                 csvwriter.writerow(header)
                 count += 1
          csvwriter.writerow(emp.values())
    

    Make sure to close the file in order to save the contents

    employ_data.close()
    

提交回复
热议问题