我在使用Python时遇到以下问题。
给出了以下JSON对象-我想
CSV标头
firstName,lastName,managersEmail,contractStartsDate
CSV内容
firstName,lastName,managersEmail,contractStartsDate
nameOfPerson,lastNameofPerson,someManager,2000-01-01
nameOfPerson2,lastNameofPerson2,someManager2,2000-02-02
我的targetJSON.json
data = '{"details":[
{"firstName":"nameOfPerson,"lastName":"lastNameofPerson","managersEmail":"someEmail","managersName":"someManager",
"departmentName":"someDepartment",
"position":"somePosition",
"contractStartsDate":"2000-01-01",
"contractEndDate":"2000-01-01",
"company":"someCompany",
"division":"someDivision",
"preferredName":"Unknown"},
{"firstName":"nameOfPerson2","lastName":"lastNameofPerson2","managersEmail":"someEmail2","managersName":"someManager2",
"departmentName":"someDepartment2",
"position":"somePosition2",
"contractStartsDate":"2000-02-02",
"contractEndDate":"2000-02-02",
"company":"someCompany",
"division":"someDivision2",
"preferredName":"Unknown"}
]}'
我的代码如下
with open('targetJSON.json', 'r') as f:
distros_dict = json.load(f)
for distro in distros_dict:
print(distro['managersEmail'])
data_file = open("targetJSON.json", "r")
values = json.load(data_file)
data_file.close()
with open("usersData.csv", "wb") as f:
wr = csv.writer(f)
for data in values:
value = data["managersEmail"]
value = data["firstName"]
for key, value in data.iteritems():
#wr.writerow([key, value])
wr.writerow([key.encode("utf-8"), value.encode("utf-8")])
但是结果完全是乱码, CSV包含所有混合在一起的内容:-(
答案 0 :(得分:2)
将newline=""
与Python 3.x配合使用时,您需要使用csv.writer()
,对于Python 2.x版本则使用wb
。
使用给出的示例JSON,您只需要遍历标头字段并根据details
中的每个条目创建一行。例如:
import json
import csv
data = """{"details":[{"firstName":"nameOfPerson","lastName":"lastNameofPerson","managersEmail":"someEmail",
"managersName":"someManager", "departmentName":"someDepartment", "position":"somePosition", "contractStartsDate":"2000-01-01",
"contractEndDate":"2000-01-01", "company":"someCompany", "division":"someDivision", "preferredName":"Unknown"},
{"firstName":"nameOfPerson2","lastName":"lastNameofPerson2","managersEmail":"someEmail2","managersName":"someManager2",
"departmentName":"someDepartment2", "position":"somePosition2", "contractStartsDate":"2000-02-02",
"contractEndDate":"2000-02-02", "company":"someCompany", "division":"someDivision2", "preferredName":"Unknown"}
]}"""
json_data = json.loads(data)
header = ["firstName", "lastName", "managersEmail", "contractStartsDate"]
with open("usersData.csv", "w", newline="", encoding="utf-8") as f_output:
csv_output = csv.writer(f_output)
csv_output.writerow(header)
for entry in json_data["details"]:
csv_output.writerow([entry[key] for key in header])
给你
firstName,lastName,managersEmail,contractStartsDate
nameOfPerson,lastNameofPerson,someEmail,2000-01-01
nameOfPerson2,lastNameofPerson2,someEmail2,2000-02-02
如果您的JSON数据包含重复项,那么您必须先加载所有数据并删除重复项,然后才能开始写行。
或者,您可以按如下方式使用csv.DictWriter
:
import json
import csv
data = """{"details":[{"firstName":"nameOfPerson","lastName":"lastNameofPerson","managersEmail":"someEmail",
"managersName":"someManager", "departmentName":"someDepartment", "position":"somePosition", "contractStartsDate":"2000-01-01",
"contractEndDate":"2000-01-01", "company":"someCompany", "division":"someDivision", "preferredName":"Unknown"},
{"firstName":"nameOfPerson2","lastName":"lastNameofPerson2","managersEmail":"someEmail2","managersName":"someManager2",
"departmentName":"someDepartment2", "position":"somePosition2", "contractStartsDate":"2000-02-02",
"contractEndDate":"2000-02-02", "company":"someCompany", "division":"someDivision2", "preferredName":"Unknown"}
]}"""
json_data = json.loads(data)
fieldnames = ["firstName", "lastName", "managersEmail", "contractStartsDate"]
with open("usersData.csv", "w", newline="", encoding="utf-8") as f_output:
csv_output = csv.DictWriter(f_output, fieldnames=fieldnames, extrasaction="ignore")
csv_output.writeheader()
csv_output.writerows(json_data["details"])
要从输入的JSON文件中读取数据,您可以执行以下操作:
import json
import csv
with open("sourceJSON.json", encoding="utf-8") as f_input:
json_data = json.load(f_input)
fieldnames = ["firstName", "lastName", "managersEmail", "contractStartsDate"]
with open("usersData.csv", "w", newline="", encoding="utf-8") as f_output:
csv_output = csv.DictWriter(f_output, fieldnames=fieldnames, extrasaction="ignore")
csv_output.writeheader()
csv_output.writerows(json_data["details"])
如果需要删除相同的行,则将最后一行替换为:
csv_output.writerows(dict(t) for t in {tuple(entry.items()) : '' for entry in json_data["details"]})