How can I convert JSON to CSV?

前端 未结 26 2068
余生分开走
余生分开走 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 22:59

    I am assuming that your JSON file will decode into a list of dictionaries. First we need a function which will flatten the JSON objects:

    def flattenjson( b, delim ):
        val = {}
        for i in b.keys():
            if isinstance( b[i], dict ):
                get = flattenjson( b[i], delim )
                for j in get.keys():
                    val[ i + delim + j ] = get[j]
            else:
                val[i] = b[i]
    
        return val
    

    The result of running this snippet on your JSON object:

    flattenjson( {
        "pk": 22, 
        "model": "auth.permission", 
        "fields": {
          "codename": "add_message", 
          "name": "Can add message", 
          "content_type": 8
        }
      }, "__" )
    

    is

    {
        "pk": 22, 
        "model": "auth.permission', 
        "fields__codename": "add_message", 
        "fields__name": "Can add message", 
        "fields__content_type": 8
    }
    

    After applying this function to each dict in the input array of JSON objects:

    input = map( lambda x: flattenjson( x, "__" ), input )
    

    and finding the relevant column names:

    columns = [ x for row in input for x in row.keys() ]
    columns = list( set( columns ) )
    

    it's not hard to run this through the csv module:

    with open( fname, 'wb' ) as out_file:
        csv_w = csv.writer( out_file )
        csv_w.writerow( columns )
    
        for i_r in input:
            csv_w.writerow( map( lambda x: i_r.get( x, "" ), columns ) )
    

    I hope this helps!

提交回复
热议问题