python - Python 将 JSON 转换为 CSV 类型错误
问题描述
我正在尝试从来自 PushShift API 但遇到 TypeError 的 JSON 数据编写 CSV 文件。我的代码如下
import requests
import csv
import json
from urllib.request import urlopen
url = 'https://api.pushshift.io/reddit/comment/search/?subreddit=science&filter=parent_id,id,author,created_utc,subreddit,body,score,permalink'
page = requests.get(url)
page_json = json.loads(page.text)
print(page.text)
f = csv.writer(open("test.csv",'w+', newline=''))
f.writerow(["id", "parent_id", "author", "created_utc","subreddit", "body", "score"])
for x in page_json:
f.writerow([x["data"]["id"],
x["data"]["parent_id"],
x["data"]["author"],
x["data"]["created_utc"],
x["data"]["subreddit"],
x["data"]["body"],
x["data"]["score"]])
我得到的错误是:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-3-82784a93576b> in <module>()
11 f.writerow(["id", "parent_id", "author", "created_utc","subreddit", "body", "score"])
12 for x in page:
---> 13 f.writerow([x["data"]["id"],
14 x["data"]["parent_id"],
15 x["data"]["author"],
TypeError: byte indices must be integers or slices, not str
我在这里尝试了解决方案:如何将 JSON 转换为 CSV?
这可能是也可能不是我遇到的实际问题。任何建议将不胜感激!
解决方案
您有“数据”,其中包含 csv 行的条目数组,而不是每个都带有键“数据”的对象数组。所以你需要先访问“数据”:
page_json = json.loads(page.text)['data']
然后迭代它:
for x in page_json:
f.writerow([x["id"],
x["parent_id"],
x["author"],
x["created_utc"],
x["subreddit"],
x["body"],
x["score"]])
请注意,您需要遍历 JSON 对象而不是请求。
你也可以重构代码来得到这个:
columns = ["id", "parent_id", "author", "created_utc", "subreddit", "body", "score"]
f.writerow(columns)
for x in page_json:
f.writerow([x[column] for column in columns])
推荐阅读
- python - Tensorflow 与 Tensorflow Lite 的性能对比
- java - 忽略请求的Wiremock问题,除了一个
- r - 使用 group_by 在行组之间进行划分
- c# - 如何从不支持列表返回类型的另一个类调用静态列表
- javascript - 如何在循环数组中给 axios 一个延迟?
- jcl - 如何使用 JCL 将一个值从数据集复制到另一个数据集
- excel - excel vba比较2列并列出不匹配的结果
- android - 原生广告星级评分恢复为 0.0?
- c++ - 带有 VIM 环绕插件的 C++ 块
- google-apps-script - 正则表达式中的 Google 表格活动列引用