简体   繁体   English

将 JSON 数组解析为 csv 文件

[英]Parse JSON array into csv file

I have a JSON response in the form of a JSON object from a request in the pattern:我有一个 JSON 响应,形式为 JSON object 来自模式中的请求:

{"a":[1,2,3,4,5],"b":[I,II,III,IV,V],"c":[p,q,r,s,t]}

How can I parse this json object into a csv file in python containing a,b,c as column names and the values as data in rows as:如何将此json object 解析为 python 中的python文件,其中包含a,b,c作为列名,值作为行中的数据:

a    b      c
1    I      p
2    II     q
3    III    r
4    IV     s
5    V      t

Code for json response: json 响应代码:

url="some url"

page=requests.get(url)
output = page.json()

The closest answer I got to was in How can I convert JSON to CSV?我得到的最接近的答案是如何将 JSON 转换为 CSV?

I have tried to convert it into a pandas dataframe and iterate through it but I can't get the work around with it with my JSON object.我试图将它转换为 pandas dataframe并遍历它,但我无法使用我的 JSON object 解决它。

pip3 install pandas

Install the Pandas library with the above command.使用上述命令安装 Pandas 库。

import pandas as pd

output = {
    "a": [1, 2, 3, 4, 5],
    "b": ["I", "II", "III", "IV", "V"],
    "c": ["p", "q", "r", "s", "t"],
}

df = pd.DataFrame(output)
df.to_csv("filename.csv", index=False, encoding="utf-8")

Output: Output:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM