How can I convert JSON to CSV?

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

    First, your JSON has nested objects, so it normally cannot be directly converted to CSV. You need to change that to something like this:

    {
        "pk": 22,
        "model": "auth.permission",
        "codename": "add_logentry",
        "content_type": 8,
        "name": "Can add log entry"
    },
    ......]
    

    Here is my code to generate CSV from that:

    import csv
    import json
    
    x = """[
        {
            "pk": 22,
            "model": "auth.permission",
            "fields": {
                "codename": "add_logentry",
                "name": "Can add log entry",
                "content_type": 8
            }
        },
        {
            "pk": 23,
            "model": "auth.permission",
            "fields": {
                "codename": "change_logentry",
                "name": "Can change log entry",
                "content_type": 8
            }
        },
        {
            "pk": 24,
            "model": "auth.permission",
            "fields": {
                "codename": "delete_logentry",
                "name": "Can delete log entry",
                "content_type": 8
            }
        }
    ]"""
    
    x = json.loads(x)
    
    f = csv.writer(open("test.csv", "wb+"))
    
    # Write CSV Header, If you dont need that, remove this line
    f.writerow(["pk", "model", "codename", "name", "content_type"])
    
    for x in x:
        f.writerow([x["pk"],
                    x["model"],
                    x["fields"]["codename"],
                    x["fields"]["name"],
                    x["fields"]["content_type"]])
    

    You will get output as:

    pk,model,codename,name,content_type
    22,auth.permission,add_logentry,Can add log entry,8
    23,auth.permission,change_logentry,Can change log entry,8
    24,auth.permission,delete_logentry,Can delete log entry,8
    
    0 讨论(0)
  • 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)
    
    0 讨论(0)
  • 2020-11-21 23:07

    This code should work for you, assuming that your JSON data is in a file called data.json.

    import json
    import csv
    
    with open("data.json") as file:
        data = json.load(file)
    
    with open("data.csv", "w") as file:
        csv_file = csv.writer(file)
        for item in data:
            fields = list(item['fields'].values())
            csv_file.writerow([item['pk'], item['model']] + fields)
    
    0 讨论(0)
  • 2020-11-21 23:07

    This works relatively well. It flattens the json to write it to a csv file. Nested elements are managed :)

    That's for python 3

    import json
    
    o = json.loads('your json string') # Be careful, o must be a list, each of its objects will make a line of the csv.
    
    def flatten(o, k='/'):
        global l, c_line
        if isinstance(o, dict):
            for key, value in o.items():
                flatten(value, k + '/' + key)
        elif isinstance(o, list):
            for ov in o:
                flatten(ov, '')
        elif isinstance(o, str):
            o = o.replace('\r',' ').replace('\n',' ').replace(';', ',')
            if not k in l:
                l[k]={}
            l[k][c_line]=o
    
    def render_csv(l):
        ftime = True
    
        for i in range(100): #len(l[list(l.keys())[0]])
            for k in l:
                if ftime :
                    print('%s;' % k, end='')
                    continue
                v = l[k]
                try:
                    print('%s;' % v[i], end='')
                except:
                    print(';', end='')
            print()
            ftime = False
            i = 0
    
    def json_to_csv(object_list):
        global l, c_line
        l = {}
        c_line = 0
        for ov in object_list : # Assumes json is a list of objects
            flatten(ov)
            c_line += 1
        render_csv(l)
    
    json_to_csv(o)
    

    enjoy.

    0 讨论(0)
  • 2020-11-21 23:08

    A generic solution which translates any json list of flat objects to csv.

    Pass the input.json file as first argument on command line.

    import csv, json, sys
    
    input = open(sys.argv[1])
    data = json.load(input)
    input.close()
    
    output = csv.writer(sys.stdout)
    
    output.writerow(data[0].keys())  # header row
    
    for row in data:
        output.writerow(row.values())
    
    0 讨论(0)
  • 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()
    
    0 讨论(0)
提交回复
热议问题