如何将JSON转换为CSV?

时间:2009-12-09 04:06:38

标签: python json csv

我有一个JSON文件,我想将其转换为CSV文件。我怎么能用Python做到这一点?

我试过了:

import json
import csv

f = open('data.json')
data = json.load(f)
f.close()
f = open('data.csv')
csv_file = csv.writer(f)
for item in data:
    f.writerow(item)

f.close()

然而,它没有用。我正在使用Django,我收到的错误是:

file' object has no attribute 'writerow'

所以,然后我尝试了以下内容:

import json
import csv

f = open('data.json')
data = json.load(f)
f.close()

f = open('data.csv')
csv_file = csv.writer(f)
for item in data:
    csv_file.writerow(item)

f.close()

然后我得到错误:

sequence expected

示例json文件:

[
  {
    "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
    }
  },
  {
    "pk": 4,
    "model": "auth.permission",
    "fields": {
      "codename": "add_group",
      "name": "Can add group",
      "content_type": 2
    }
  },
  {
    "pk": 10,
    "model": "auth.permission",
    "fields": {
      "codename": "add_message",
      "name": "Can add message",
      "content_type": 4
    }
  }
]

26 个答案:

答案 0 :(得分:107)

我不确定这个问题是否已经解决,但是让我粘贴我所做的以供参考。

首先,您的JSON具有嵌套对象,因此通常无法直接转换为CSV。 您需要将其更改为以下内容:

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

以下是我从中生成CSV的代码:

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"]])

您将获得输出:

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

答案 1 :(得分:78)

我假设您的JSON文件将解码为字典列表。首先,我们需要一个能够展平JSON对象的函数:

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

在JSON对象上运行此代码段的结果:

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

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

将此函数应用于JSON对象的输入数组中的每个dict后:

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

并找到相关的列名:

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

通过csv模块运行它并不困难:

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 ) )

我希望这有帮助!

答案 2 :(得分:70)

使用pandas library这就像使用两个命令一样简单!

pandas.read_json()

将JSON字符串转换为pandas对象(系列或数据框)。然后,假设结果存储为df

df.to_csv()

可以返回字符串或直接写入csv文件。

基于先前答案的详细程度,我们都应该感谢大熊猫的捷径。

答案 3 :(得分:34)

JSON可以表示各种各样的数据结构 - JS“对象”大致类似于Python dict(带字符串键),JS“数组”大致类似于Python列表,你可以嵌套它们只要最后的“叶子”元素是数字或字符串。

CSV基本上只能表示一个二维表 - 可选地带有第一行“标题”,即“列名”,它可以使表可解释为一个字典列表,而不是正常的解释,列表列表(同样,“叶子”元素可以是数字或字符串)。

因此,在一般情况下,您无法将任意JSON结构转换为CSV。在一些特殊情况下,您可以(没有进一步嵌套的数组数组;所有具有完全相同键的对象数组)。哪种特殊情况(如果有的话)适用于您的问题?解决方案的细节取决于您拥有的特殊情况。鉴于您甚至没有提到哪一个适用的惊人事实,我怀疑您可能没有考虑过约束,事实上既不适用也不适用,而您的问题无法解决。但请澄清!

答案 4 :(得分:25)

flat 对象的任何json列表转换为csv的通用解决方案。

将input.json文件作为命令行的第一个参数传递。

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())

答案 5 :(得分:21)

此代码应该适合您,假设您的JSON数据位于名为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:
        csv_file.writerow([item['pk'], item['model']] + item['fields'].values())

答案 6 :(得分:15)

它易于使用<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.1.3/vue.js"></script> <div id="example"> <template v-for="selector in selectors"> <select v-model="selected[ selector.name ]"> <option v-for="option in selector.options">{{option}}</option> </select> </template> <p> {{selected.make}} <br /> {{selected.fuel}} </p> </div>,具体实现可以是这样的:

csv.DictWriter()

请注意,这假设您的所有JSON对象都具有相同的字段。

以下reference可能会对您有所帮助。

答案 7 :(得分:6)

我遇到了Dan's proposed solution的问题,但这对我有用:

import json
import csv 

f = open('test.json')
data = json.load(f)
f.close()

f=csv.writer(open('test.csv','wb+'))

for item in data:
  f.writerow([item['pk'], item['model']] + item['fields'].values())

“test.json”包含以下内容:

[ 
{"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 } }
]

答案 8 :(得分:4)

我知道自从提出这个问题以来已经有很长一段时间了,但我想我可能会添加其他人的答案并分享一篇博文,我认为这篇文章会以非常简洁的方式解释。

以下是link

打开要写入的文件

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

创建csv编写器对象

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())

确保关闭文件以保存内容

employ_data.close()

答案 9 :(得分:4)

如前面的答案所述,将json转换为csv的难度是因为json文件可以包含嵌套字典,因此是一个多维数据结构,而不是csv,它是一个2D数据结构。但是,将多维结构转换为csv的好方法是将多个csv与主键绑定在一起。

在您的示例中,第一个csv输出的列为“pk”,“model”,“fields”。 “pk”和“model”的值很容易获得,但因为“fields”列包含一个字典,它应该是它自己的csv,因为“codename”看起来是主键,你可以用作输入为“领域”完成第一个csv。第二个csv包含来自“fields”列的字典,其中代号为主键,可用于将2个csv绑定在一起。

这是一个json文件的解决方案,它将嵌套的字典转换为2个csvs。

import csv
import json

def readAndWrite(inputFileName, primaryKey=""):
    input = open(inputFileName+".json")
    data = json.load(input)
    input.close()

    header = set()

    if primaryKey != "":
        outputFileName = inputFileName+"-"+primaryKey
        if inputFileName == "data":
            for i in data:
                for j in i["fields"].keys():
                    if j not in header:
                        header.add(j)
    else:
        outputFileName = inputFileName
        for i in data:
            for j in i.keys():
                if j not in header:
                    header.add(j)

    with open(outputFileName+".csv", 'wb') as output_file:
        fieldnames = list(header)
        writer = csv.DictWriter(output_file, fieldnames, delimiter=',', quotechar='"')
        writer.writeheader()
        for x in data:
            row_value = {}
            if primaryKey == "":
                for y in x.keys():
                    yValue = x.get(y)
                    if type(yValue) == int or type(yValue) == bool or type(yValue) == float or type(yValue) == list:
                        row_value[y] = str(yValue).encode('utf8')
                    elif type(yValue) != dict:
                        row_value[y] = yValue.encode('utf8')
                    else:
                        if inputFileName == "data":
                            row_value[y] = yValue["codename"].encode('utf8')
                            readAndWrite(inputFileName, primaryKey="codename")
                writer.writerow(row_value)
            elif primaryKey == "codename":
                for y in x["fields"].keys():
                    yValue = x["fields"].get(y)
                    if type(yValue) == int or type(yValue) == bool or type(yValue) == float or type(yValue) == list:
                        row_value[y] = str(yValue).encode('utf8')
                    elif type(yValue) != dict:
                        row_value[y] = yValue.encode('utf8')
                writer.writerow(row_value)

readAndWrite("data")

答案 10 :(得分:3)

使用json_normalize中的pandas

  • 在名为test.json的文件中提供所提供的数据
  • encoding='utf-8'可能不是必需的。
  • 以下代码利用pathlib
    • .openpathlib
    • 的方法
    • 也适用于非Windows路径
import pandas as pd
from pandas.io.json import json_normalize
from pathlib import Path
import json

# set path to file
p = Path(r'c:\some_path_to_file\test.json')

# read json
with p.open('r', encoding='utf-8') as f:
    data = json.loads(f.read())

# create dataframe
df = json_normalize(data)

# dataframe view
 pk            model  fields.codename           fields.name  fields.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
  4  auth.permission        add_group         Can add group                    2
 10  auth.permission      add_message       Can add message                    4

# save to csv
df.to_csv('test.csv', index=False, encoding='utf-8')

CSV输出:

pk,model,fields.codename,fields.name,fields.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
4,auth.permission,add_group,Can add group,2
10,auth.permission,add_message,Can add message,4

用于更多嵌套大量JSON对象的其他资源:

答案 11 :(得分:2)

我解决这个问题的简单方法:

创建一个新的Python文件,如:json_to_csv.py

添加此代码:

import csv, json, sys
#if you are not using utf-8 files, remove the next line
sys.setdefaultencoding("UTF-8")
#check if you pass the input file and output file
if sys.argv[1] is not None and sys.argv[2] is not None:

    fileInput = sys.argv[1]
    fileOutput = sys.argv[2]

    inputFile = open(fileInput)
    outputFile = open(fileOutput, 'w')
    data = json.load(inputFile)
    inputFile.close()

    output = csv.writer(outputFile)

    output.writerow(data[0].keys())  # header row

    for row in data:
        output.writerow(row.values())

添加此代码后,保存文件并在终端上运行:

  

python json_to_csv.py input.txt output.csv

我希望这对你有所帮助。

SEEYA!

答案 12 :(得分:2)

这个效果相对较好。 它会使json变平,将其写入csv文件。 管理嵌套元素:)

这是针对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)

享受。

答案 13 :(得分:2)

这不是一个非常聪明的方法,但我遇到了同样的问题,这对我有用:

import csv

f = open('data.json')
data = json.load(f)
f.close()

new_data = []

for i in data:
   flat = {}
   names = i.keys()
   for n in names:
      try:
         if len(i[n].keys()) > 0:
            for ii in i[n].keys():
               flat[n+"_"+ii] = i[n][ii]
      except:
         flat[n] = i[n]
   new_data.append(flat)  

f = open(filename, "r")
writer = csv.DictWriter(f, new_data[0].keys())
writer.writeheader()
for row in new_data:
   writer.writerow(row)
f.close()

答案 14 :(得分:1)

修改了Alec McGail的答案,用

中的列表支持JSON
    def flattenjson(self, mp, delim="|"):
            ret = []
            if isinstance(mp, dict):
                    for k in mp.keys():
                            csvs = self.flattenjson(mp[k], delim)
                            for csv in csvs:
                                    ret.append(k + delim + csv)
            elif isinstance(mp, list):
                    for k in mp:
                            csvs = self.flattenjson(k, delim)
                            for csv in csvs:
                                    ret.append(csv)
            else:
                    ret.append(mp)

            return ret

谢谢!

答案 15 :(得分:1)

如果我们考虑以下将json格式文件转换为csv格式文件的示例。

{
 "item_data" : [
      {
        "item": "10023456",
        "class": "100",
        "subclass": "123"
      }
      ]
}

下面的代码会将json文件(data3.json)转换为csv文件(data3.csv)。

import json
import csv
with open("/Users/Desktop/json/data3.json") as file:
    data = json.load(file)
    file.close()
    print(data)

fname = "/Users/Desktop/json/data3.csv"

with open(fname, "w", newline='') as file:
    csv_file = csv.writer(file)
    csv_file.writerow(['dept',
                       'class',
                       'subclass'])
    for item in data["item_data"]:
         csv_file.writerow([item.get('item_data').get('dept'),
                            item.get('item_data').get('class'),
                            item.get('item_data').get('subclass')])

上述代码已在本地安装的pycharm中执行,并且已成功将json文件转换为csv文件。希望对转换文件有帮助。

答案 16 :(得分:1)

import json,csv
t=''
t=(type('a'))
json_data = []
data = None
write_header = True
item_keys = []
try:
with open('kk.json') as json_file:
    json_data = json_file.read()

    data = json.loads(json_data)
except Exception as e:
    print( e)

with open('bar.csv', 'at') as csv_file:
    writer = csv.writer(csv_file)#, quoting=csv.QUOTE_MINIMAL)
    for item in data:
        item_values = []
        for key in item:
            if write_header:
                item_keys.append(key)
            value = item.get(key, '')
            if (type(value)==t):
                item_values.append(value.encode('utf-8'))
            else:
                item_values.append(value)
        if write_header:
            writer.writerow(item_keys)
            write_header = False
        writer.writerow(item_values)

答案 17 :(得分:1)

尝试一下

from iminuit import Minuit
from math import pi
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline 

def logis(x,a,b): #function to fit
    return (1+x*np.log(x/b))*a

xlat=np.array([81.96100485,  64.96427609,  38.15520137,  45.75992993,
        27.38344029,  23.41742996,  18.73586921,  18.07486749,
         9.20589292,   4.83878931,  72.17070899,  71.08083681,
        39.57647386,  31.63373626]) #xdata

lo=logis(xlat,92,30) #ydata
p=np.concatenate((np.array([92,30]),xlat,lo),axis=0) #all parameters


def chi(pa): #chisquare
    chis=sum((92-pa[0:1])**2)+sum((30-pa[1:2])**2)+sum((xlat-pa[2:16])**2)+sum((lo-logis(pa[2:16],92,30))**2)
    return chis

#minuit part
m = Minuit.from_array_func(chi,p,errordef=1)

fmin,para=m.migrad()

print(m.values)
print(fmin)

xn=[]
xtra=np.sort(xlat)
for i in range(2,16):
    xn.append(m.values[i])

xr=np.array(xn)
#plot part
xp=np.linspace(min(xr),max(xr),14)
plt.figure(figsize=(14,8.5))
plt.plot(xp,logis(xp,m.values[0],m.values[1]))
plt.show

答案 18 :(得分:0)

这是@MikeRepass答案的修改。此版本将CSV写入文件,并且适用于Python 2和Python 3。

import csv,json
input_file="data.json"
output_file="data.csv"
with open(input_file) as f:
    content=json.load(f)
try:
    context=open(output_file,'w',newline='') # Python 3
except TypeError:
    context=open(output_file,'wb') # Python 2
with context as file:
    writer=csv.writer(file)
    writer.writerow(content[0].keys()) # header row
    for row in content:
        writer.writerow(row.values())

答案 19 :(得分:0)

我可能迟到了聚会,但我认为,我已经解决了类似的问题。我有一个看起来像这样的json文件

JSON File Structure

我只想从这些json文件中提取一些键/值。因此,我编写了以下代码以提取相同的代码。

    """json_to_csv.py
    This script reads n numbers of json files present in a folder and then extract certain data from each file and write in a csv file.
    The folder contains the python script i.e. json_to_csv.py, output.csv and another folder descriptions containing all the json files.
"""

import os
import json
import csv


def get_list_of_json_files():
    """Returns the list of filenames of all the Json files present in the folder
    Parameter
    ---------
    directory : str
        'descriptions' in this case
    Returns
    -------
    list_of_files: list
        List of the filenames of all the json files
    """

    list_of_files = os.listdir('descriptions')  # creates list of all the files in the folder

    return list_of_files


def create_list_from_json(jsonfile):
    """Returns a list of the extracted items from json file in the same order we need it.
    Parameter
    _________
    jsonfile : json
        The json file containing the data
    Returns
    -------
    one_sample_list : list
        The list of the extracted items needed for the final csv
    """

    with open(jsonfile) as f:
        data = json.load(f)

    data_list = []  # create an empty list

    # append the items to the list in the same order.
    data_list.append(data['_id'])
    data_list.append(data['_modelType'])
    data_list.append(data['creator']['_id'])
    data_list.append(data['creator']['name'])
    data_list.append(data['dataset']['_accessLevel'])
    data_list.append(data['dataset']['_id'])
    data_list.append(data['dataset']['description'])
    data_list.append(data['dataset']['name'])
    data_list.append(data['meta']['acquisition']['image_type'])
    data_list.append(data['meta']['acquisition']['pixelsX'])
    data_list.append(data['meta']['acquisition']['pixelsY'])
    data_list.append(data['meta']['clinical']['age_approx'])
    data_list.append(data['meta']['clinical']['benign_malignant'])
    data_list.append(data['meta']['clinical']['diagnosis'])
    data_list.append(data['meta']['clinical']['diagnosis_confirm_type'])
    data_list.append(data['meta']['clinical']['melanocytic'])
    data_list.append(data['meta']['clinical']['sex'])
    data_list.append(data['meta']['unstructured']['diagnosis'])
    # In few json files, the race was not there so using KeyError exception to add '' at the place
    try:
        data_list.append(data['meta']['unstructured']['race'])
    except KeyError:
        data_list.append("")  # will add an empty string in case race is not there.
    data_list.append(data['name'])

    return data_list


def write_csv():
    """Creates the desired csv file
    Parameters
    __________
    list_of_files : file
        The list created by get_list_of_json_files() method
    result.csv : csv
        The csv file containing the header only
    Returns
    _______
    result.csv : csv
        The desired csv file
    """

    list_of_files = get_list_of_json_files()
    for file in list_of_files:
        row = create_list_from_json(f'descriptions/{file}')  # create the row to be added to csv for each file (json-file)
        with open('output.csv', 'a') as c:
            writer = csv.writer(c)
            writer.writerow(row)
        c.close()


if __name__ == '__main__':
    write_csv()

我希望这会有所帮助。有关此代码的工作方式的详细信息,请检查here

答案 20 :(得分:0)

Alec's answer很不错,但是在存在多层嵌套的情况下,它不起作用。这是一个支持多个嵌套级别的修改版本。如果嵌套对象已经指定了自己的键(例如Firebase Analytics / BigTable / BigQuery数据),它也使标头名称更好一些:

"""Converts JSON with nested fields into a flattened CSV file.
"""

import sys
import json
import csv
import os

import jsonlines

from orderedset import OrderedSet

# from https://stackoverflow.com/a/28246154/473201
def flattenjson( b, prefix='', delim='/', val={} ):
  if isinstance( b, dict ):
    for j in b.keys():
      flattenjson(b[j], prefix + delim + j, delim, val)
  elif isinstance( b, list ):
    get = b
    for j in range(len(get)):
      key = str(j)

      # If the nested data contains its own key, use that as the header instead.
      if isinstance( get[j], dict ):
        if 'key' in get[j]:
          key = get[j]['key']

      flattenjson(get[j], prefix + delim + key, delim, val)
  else:
    val[prefix] = b

  return val

def main(argv):
  if len(argv) < 2:
    raise Error('Please specify a JSON file to parse')

  filename = argv[1]
  allRows = []
  fieldnames = OrderedSet()
  with jsonlines.open(filename) as reader:
    for obj in reader:
      #print obj
      flattened = flattenjson(obj)
      #print 'keys: %s' % flattened.keys()
      fieldnames.update(flattened.keys())
      allRows.append(flattened)

  outfilename = filename + '.csv'
  with open(outfilename, 'w') as file:
    csvwriter = csv.DictWriter(file, fieldnames=fieldnames)
    csvwriter.writeheader()
    for obj in allRows:
      csvwriter.writerow(obj)



if __name__ == '__main__':
  main(sys.argv)

答案 21 :(得分:0)

此代码适用于任何给定的json文件

# -*- coding: utf-8 -*-
"""
Created on Mon Jun 17 20:35:35 2019
author: Ram
"""

import json
import csv

with open("file1.json") as file:
    data = json.load(file)



# create the csv writer object
pt_data1 = open('pt_data1.csv', 'w')
csvwriter = csv.writer(pt_data1)

count = 0

for pt in data:

      if count == 0:

             header = pt.keys()

             csvwriter.writerow(header)

             count += 1

      csvwriter.writerow(pt.values())

pt_data1.close()

答案 22 :(得分:0)

您可以使用此代码将json文件转换为csv文件 读取文件后,我将对象转换为pandas数据框,然后将其保存为CSV文件

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)

答案 23 :(得分:0)

Surprisingly, I found that none of the answers posted here so far correctly deal with all possible scenarios (e.g., nested dicts, nested lists, None values, etc).

This solution should work across all scenarios:

def flatten_json(json):
    def process_value(keys, value, flattened):
        if isinstance(value, dict):
            for key in value.keys():
                process_value(keys + [key], value[key], flattened)
        elif isinstance(value, list):
            for idx, v in enumerate(value):
                process_value(keys + [str(idx)], v, flattened)
        else:
            flattened['__'.join(keys)] = value

    flattened = {}
    for key in json.keys():
        process_value([key], json[key], flattened)
    return flattened

答案 24 :(得分:0)

不幸的是,我对获得惊人的@Alec McGail答案贡献不大。 我使用的是Python3,我需要将地图转换为@Alexis R注释后的列表。

另外,我发现csv编写器正在向文件添加一个额外的CR(我在csv文件中的每一行都有一个空行,其中包含数据)。 @Jason R. Coombs对这个线程的回答之后,解决方案非常简单: CSV in Python adding an extra carriage return

您只需将lineterminator ='\ n'参数添加到csv.writer。它将是:csv_w = csv.writer( out_file, lineterminator='\n' )

答案 25 :(得分:0)

由于数据看起来是字典格式,看起来您应该实际使用csv.DictWriter()来实际输出具有相应标题信息的行。这应该允许稍微更容易地处理转换。然后,fieldnames参数将正确设置顺序,而第一行的输出作为标题将允许稍后由csv.DictReader()读取和处理。

例如,Mike Repass使用

output = csv.writer(sys.stdout)

output.writerow(data[0].keys())  # header row

for row in data:
  output.writerow(row.values())

然而,只需将初始设置更改为     output = csv.DictWriter(filesetting,fieldnames = data [0] .keys())

请注意,由于未定义字典中元素的顺序,因此可能必须显式创建字段名称条目。一旦你这样做,作家将会工作。然后写入按原始显示的方式工作。