dimanche 17 janvier 2016

Python JSON data importing

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()

Aucun commentaire:

Enregistrer un commentaire