【问题标题】:Parse JSON to CSV + additional columns将 JSON 解析为 CSV + 附加列
【发布时间】:2021-07-29 03:51:19
【问题描述】:

我正在尝试将具有以下语法的 JSON 文件解析为 CSV:

{"code":2000,"message":"SUCCESS","data":
{"1":
  {"id":1,
"name":"first_name",
"icon":"url.png",
"attribute1":"value",
"attribute2":"value" ...},
"2":
  {"id":2,
"name":"first_name",
"icon":"url.png",
"attribute1":"value",
"attribute2":"value" ...},
"3":
  {"id":3,
"name":"first_name",
"icon":"url.png",
"attribute1":"value",
"attribute2":"value" ...}, and so forth
}}}

我发现了类似的问题(例如herehere,我正在使用以下方法:

import requests
import json
import csv
import os

jsonfile = "/path/to.json"
csvfile = "/path/to.csv"

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

data_file = open(csvfile,'w')

csvwriter = csv.writer(data_file)
csvwriter.writerow(data["data"].keys())

for row in data:
    csvwriter.writerow(row["data"].values())
data_file.close()

但我错过了一些东西。 当我尝试运行时出现此错误:

TypeError: string indices must be integers

我的 csv 输出是:

1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,96

最后,我正在尝试将以下函数(来自 PowerShell)转换为 Python。这将 JSON 转换为 CSV,并在末尾添加了 3 个额外的自定义列:

$json = wget $lvl | ConvertFrom-Json
$json.data | %{$_.psobject.properties.value} `
   | select-object *,@{Name='Custom1';Expression={$m}},@{Name='Level';Expression={$l}},@{Name='Custom2';Expression={$a}},@{Name='Custom3';Expression={$r}} `
   | Export-CSV -path $outfile

输出如下:

"id","name","icon","attribute1","attribute2",..."Custom1","Custom2","Custom3"
"1","first_name","url.png","value","value",..."a","b","c"
"2","first_name","url.png","value","value",..."a","b","c"
"3","first_name","url.png","value","value",..."a","b","c"

【问题讨论】:

  • 请提供一个可运行的minimal reproducible example,包括示例输入数据和整个回溯(不仅仅是TypeError这一行。
  • $m$l 等这些变量在哪里定义?

标签: python json powershell csv


【解决方案1】:

作为martineausuggested 在一个现已删除的答案中,我的密钥名称不正确。

我最终得到了这个:

import json
import csv

jsonfile = "/path/to.json"
csvfile = "/path/to.csv"

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

data_file = open(csvfile,'w')

csvwriter = csv.writer(data_file)

#get sample keys
header=data["data"]["1"].keys()

#add new fields to dict
keys = list(header)
keys.append("field2")
keys.append("field3")

#write header
csvwriter.writerow(keys)

#for each entry
total = data["data"]

for row in total:
    rowdefault = data["data"][str(row)].values()
    rowdata = list(rowdefault)
    rowdata.append("value1")
    rowdata.append("value2")
    csvwriter.writerow(rowdata)

在这里,我通过 str(row) 的名称 id 获取每一行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 2017-07-31
    • 2014-07-30
    • 1970-01-01
    • 1970-01-01
    • 2018-02-09
    • 2021-08-31
    相关资源
    最近更新 更多