使用Python将文本文件中的Json数组转换为CSV

问题描述 投票:-1回答:1

我正在尝试将存储在文本文件中的数组转换为CSV格式。我收到错误消息“ TypeError:字符串索引必须为整数”我假设错误是由于Json结构不正确造成的?我该如何解决?

Python:

import csv
import json

with open("c://JsonData/File.txt") as x:
    data = json.dumps(x.read())

x = data
f = csv.writer(open("Export.csv", "w", newline=''))
f.writerow(["firstName", "lastName", "phoneNum", "city", "state", "zip"])

for x in x:
    f.writerow([x["Employee"]["firstName"],
                x["Employee"]["lastName"],
                x["Employee"]["phoneNum"],
                x["Employee"]["city"],
                x["Employee"]["state"],
                x["Employee"]["zip"])   

文本文件中的数组:

{
"Employee":             
[ {
      "firstName" : "Sam",
      "lastName" : "Tucker",
      "phoneNum" : "555-6789",
      "city" : "Mayberry",
      "state" : "NC",
      "zip" : "35083",
    }, {
      "firstName" : "Johny Paul",
      "lastName" : "Jason",
      "phoneNum" : "555-6769",
      "city" : "Mayberry",
      "state" : "NC",
      "zip" : "35083",
    }, {
      "firstName" : "Rafe",
      "lastName" : "Holister",
      "phoneNum" : "555-6489",
      "city" : "Mayberry",
      "state" : "NC",
      "zip" : "35083",",
    } ]
}

我也尝试过这个:

for x in x:
    f.writerow([x["firstName"],
                x["lastName"],
                x["phoneNum"],
                x["city"],
                x["state"],
                x["zip"])

数组的格式如下:

[ {
      "firstName" : "Sam",
      "lastName" : "Tucker",
      "phoneNum" : "555-6789",
      "city" : "Mayberry",
      "state" : "NC",
      "zip" : "35083",
    }, {
      "firstName" : "Johny Paul",
      "lastName" : "Jason",
      "phoneNum" : "555-6769",
      "city" : "Mayberry",
      "state" : "NC",
      "zip" : "35083",
    }, {
      "firstName" : "Rafe",
      "lastName" : "Holister",
      "phoneNum" : "555-6489",
      "city" : "Mayberry",
      "state" : "NC",
      "zip" : "35083",",
    } ]
python
1个回答
0
投票
import csv import json with open("data.json", "r") as file: data = json.load(file) with open("data.csv", "w") as file: file = csv.writer(file) file.writerow(["firstName","lastName","phoneNum","city","state","zip"]) for d in data: file.writerow([d["firstName"],d["lastName"],d["phoneNum"], d["city"],d["state"],d["zip"]])

当应用于以下JSON文件“ data.json”时

[ {
      "firstName" : "Sam",
      "lastName" : "Tucker",
      "phoneNum" : "555-6789",
      "city" : "Mayberry",
      "state" : "NC",
      "zip" : "35083"
    }, {
      "firstName" : "Johny Paul",
      "lastName" : "Jason",
      "phoneNum" : "555-6769",
      "city" : "Mayberry",
      "state" : "NC",
      "zip" : "35083"
    }, {
      "firstName" : "Rafe",
      "lastName" : "Holister",
      "phoneNum" : "555-6489",
      "city" : "Mayberry",
      "state" : "NC",
      "zip" : "35083"
    }
]

它将生成以下CSV文件“ data.csv”:

firstName,lastName,phoneNum,city,state,zip
Sam,Tucker,555-6789,Mayberry,NC,35083
Johny Paul,Jason,555-6769,Mayberry,NC,35083
Rafe,Holister,555-6489,Mayberry,NC,35083
© www.soinside.com 2019 - 2024. All rights reserved.