I am trying to import data from a json file to sqlite but I got some error, I thought first that the problem is in the paths, I changed them in many ways but... no result, it gives me this error:
Traceback (most recent call last):
File "C:/Users/Taner/PycharmProjects/untitled/MyProgram.py", line 13, in <module>
json_object = json.loads(dummy_json)
File "C:\Users\Taner\AppData\Local\Programs\Python\Python35-32\lib\json\__init__.py", line 312, in loads
s.__class__.__name__))
TypeError: the JSON object must be str, not 'bytes'
and here is my code:
import json
import bz2
import sqlite3
fpath = "C:\\Users\\Taner\\Downloads\\RC_2012-12.bz2"
databasepath = "C:\\Users\\Taner\\Desktop\\Seagate\\reddit"
conn = sqlite3.connect(databasepath)
curs = conn.cursor()
with bz2.BZ2File(fpath) as file:
for line in file:
dummy_json = line
json_object = json.loads(dummy_json)
po = json.loads(line.decode('utf8'))
curs.execute("INSERT INTO Reddit VALUES (?,?,?)", (po['id'], po['subreddit_id'], po['subreddit'],))
conn.commit()
You already have the answer here, since in the line following the error you correctly decode the line from utf-8 before passing it to json.loads. The line with the error is therefore completely pointless, and you should remove it.
The correct form of your loop should be:
with bz2.BZ2File(fpath) as file:
for line in file:
po = json.loads(json.loads(line.decode('utf8')))
curs.execute("INSERT INTO Reddit VALUES (?,?,?)", (po['id'], po['subreddit_id'], po['subreddit'],))
conn.commit()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.