Python API JSON对CSV的响应
我更希望以另一种格式输出,但我正在努力获得它 代码: 输出Python API JSON对CSV的响应,python,json,pandas,csv,Python,Json,Pandas,Csv,我更希望以另一种格式输出,但我正在努力获得它 代码: 输出 一个站点的所有产品都合并到一个单元格中 想要的输出 要么: 一行中的所有产品和列中的所有站点 或所有平面,第一个场地1,所有产品为行,然后场地2,所有产品 预期产出备选方案1 预期产出备选方案2 输出的结构是: [ 范例 [{ "SiteId": "0102", "Products": [{ "ProductId": "12107708", "ProductNumber": "7
一个站点的所有产品都合并到一个单元格中 想要的输出
要么:
- 一行中的所有产品和列中的所有站点
- 或所有平面,第一个场地1,所有产品为行,然后场地2,所有产品
[{
"SiteId": "0102",
"Products": [{
"ProductId": "12107708",
"ProductNumber": "7070501"
},
{
"ProductId": "15578",
"ProductNumber": "26804"
},
{
"ProductId": "15671",
"ProductNumber": "600102"
}
]
}
]
Spyder中pprint(数据)
的输出为:
{'ProductId': '21831062', 'ProductNumber': '3364603'},
{'ProductId': '24432865', 'ProductNumber': '133101'},
{'ProductId': '24432978', 'ProductNumber': '1194515'},
{'ProductId': '1029420', 'ProductNumber': '198301'},
{'ProductId': '12282', 'ProductNumber': '408701'},
{'ProductId': '12946229', 'ProductNumber': '7174706'},
{'ProductId': '13278', 'ProductNumber': '42302'},
{'ProductId': '1028718', 'ProductNumber': '7536001'},
{'ProductId': '12945249', 'ProductNumber': '197404'},
{'ProductId': '16380', 'ProductNumber': '1133301'},
{'ProductId': '1866', 'ProductNumber': '257102'},
{'ProductId': '24420534', 'ProductNumber': '3422315'},
{'ProductId': '24424403', 'ProductNumber': '259301'},
{'ProductId': '10276', 'ProductNumber': '18004'},
{'ProductId': '1158212', 'ProductNumber': '689401'},
{'ProductId': '21775', 'ProductNumber': '395806'},
只需使用内置的CSV模块即可实现多个站点列的第一次输出。为此,您需要首先解析整个文件,以确定标题所需的所有站点。同时创建具有给定
ProductId
和ProductNumber
组合的站点列表:
from collections import defaultdict
import csv
# Sample data
json_data = [
{"SiteId" : "0102", "Products" : [{"ProductId" : "12107708", "ProductNumber" : "7070501"}, {"ProductId" : "15578", "ProductNumber" : "26804"}, {"ProductId" : "15671", "ProductNumber" : "600102"}]},
{"SiteId" : "0104", "Products" : [{"ProductId" : "12107708", "ProductNumber" : "7070501"}, {"ProductId" : "15579", "ProductNumber" : "26804"}, {"ProductId" : "15671", "ProductNumber" : "600102"}]}
]
data = defaultdict(list)
sites = set()
for site in json_data:
for product in site['Products']:
site_text = f"site {site['SiteId']}"
data[(product['ProductId'], product['ProductNumber'])].append(site_text)
sites.add(site_text)
fieldnames = ['ProductId', 'ProductNumber', *sorted(sites)]
with open('output.csv', 'w', newline='') as f_output:
csv_output = csv.DictWriter(f_output, fieldnames=fieldnames)
csv_output.writeheader()
for (id, number) in data:
row = {'ProductId' : id, 'ProductNumber' : number}
for site in data[(id, number)]:
row[site] = 1
csv_output.writerow(row)
因此,对于给定的示例数据(有两个站点),您将获得以下output.csv
output:
ProductId,ProductNumber,站点0102,站点0104
12107708,7070501,1,1
15578,26804,1,
15671,600102,1,1
15579,26804,,1
你能分享你得到的原始响应吗?这样我们就可以看到发生了什么?我不确定我是否完全理解原始响应的含义,或者如何向你展示这一点(python或Spyder没有经验)。但是如果我添加打印(数据)对于我的代码,Spyder中的回答是:您可以使用问题下方的按钮来改进您的问题。复制/粘贴打印(数据)
的输出,并将其格式化为代码,以便我们可以更好地帮助您。出于某些原因,我无法再编辑我的评论。输出非常大,因此它在此处为您提供一个片段:{“ProductId”:“25077”,“产品编号”:“694202”},{“产品ID”:“369”,“产品编号”:“44802”},{“产品ID”:“358700”,“产品编号”:“555201”},{“产品ID”:“1084111”,“产品编号”:“463801”}SiteId
来自何处?它似乎不在您的数据输出中
太好了!今天稍后将尝试它并返回给您!非常感谢!工作非常完美!!
{'ProductId': '21831062', 'ProductNumber': '3364603'},
{'ProductId': '24432865', 'ProductNumber': '133101'},
{'ProductId': '24432978', 'ProductNumber': '1194515'},
{'ProductId': '1029420', 'ProductNumber': '198301'},
{'ProductId': '12282', 'ProductNumber': '408701'},
{'ProductId': '12946229', 'ProductNumber': '7174706'},
{'ProductId': '13278', 'ProductNumber': '42302'},
{'ProductId': '1028718', 'ProductNumber': '7536001'},
{'ProductId': '12945249', 'ProductNumber': '197404'},
{'ProductId': '16380', 'ProductNumber': '1133301'},
{'ProductId': '1866', 'ProductNumber': '257102'},
{'ProductId': '24420534', 'ProductNumber': '3422315'},
{'ProductId': '24424403', 'ProductNumber': '259301'},
{'ProductId': '10276', 'ProductNumber': '18004'},
{'ProductId': '1158212', 'ProductNumber': '689401'},
{'ProductId': '21775', 'ProductNumber': '395806'},
from collections import defaultdict
import csv
# Sample data
json_data = [
{"SiteId" : "0102", "Products" : [{"ProductId" : "12107708", "ProductNumber" : "7070501"}, {"ProductId" : "15578", "ProductNumber" : "26804"}, {"ProductId" : "15671", "ProductNumber" : "600102"}]},
{"SiteId" : "0104", "Products" : [{"ProductId" : "12107708", "ProductNumber" : "7070501"}, {"ProductId" : "15579", "ProductNumber" : "26804"}, {"ProductId" : "15671", "ProductNumber" : "600102"}]}
]
data = defaultdict(list)
sites = set()
for site in json_data:
for product in site['Products']:
site_text = f"site {site['SiteId']}"
data[(product['ProductId'], product['ProductNumber'])].append(site_text)
sites.add(site_text)
fieldnames = ['ProductId', 'ProductNumber', *sorted(sites)]
with open('output.csv', 'w', newline='') as f_output:
csv_output = csv.DictWriter(f_output, fieldnames=fieldnames)
csv_output.writeheader()
for (id, number) in data:
row = {'ProductId' : id, 'ProductNumber' : number}
for site in data[(id, number)]:
row[site] = 1
csv_output.writerow(row)