I am attempting to write a CSV file from JSON data that is coming from the PushShift API but am running into a TypeError. My code is below
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"]])
The error I am getting is this:
---------------------------------------------------------------------------
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
I attempted the solution here: How can I convert JSON to CSV?
Which may or may not be the actual problem I am running into. Any suggestions would be greatly appreciated!