【发布时间】: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
}}}
我发现了类似的问题(例如here 和here,我正在使用以下方法:
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