Python / JSON:一个接一个的错误

问题描述 投票:-2回答:1

我正在关注制作聊天机器人的在线教程......这里是开头部分的代码

import sqlite3
import json
from datetime import datetime

timeframe = '2015-01'
sql_transaction = []

connection = sqlite3.connect('{}.db'.format(timeframe))
c = connection.cursor()

def create_table():
    c.execute("CREATE TABLE IF NOT EXISTS parent_reply(parent_id TEXT PRIMARY KEY, comment_id TEXT UNIQUE, parent TEXT, comment TEXT, subreddit TEXT, unix INT, score INT)")

def format_data(data):
    data = data.replace('\n', ' newlinechar ').replace('\r', ' newlinechar ').replace('"', "'")
    return data

def find_parent(pid):
    try:
        sql = "SELECT comment FROM parent_reply WHERE comment_id = '{}' LIMIT 1".format(pid)
        c.execute(sql)
        result = c.fetchone()
        if result != None:
            return result[0]
        else: return False
    except Exception as e:
        #print(str(e))
        return False


if __name__ == '__main__':
    create_table()
    row_counter = 0
    paired_rows = 0

    with open('C:/Users/oriba/Desktop/Month of Reddit/RC_2015-01'.format(timeframe.split('-')[0], timeframe), encoding='ISO-8859-1', buffering=1000) as f:
        for row in f:
            print(row)
            row_counter += 1
            row = json.load(row)
            parent_id = row['parent_id']
            body = format_data(row['body'])
            created_utc = row['created_utc']
            score = row['score']
            comment_id = row['name']
            subreddit = row['subreddit']
            parent_data = find_parent(parent_id)

当它运行时,我收到此错误:

Traceback (most recent call last):
  File "C:/Users/oriba/Desktop/Month of Reddit/chatbot.py", line 37, in <module>
    for row in f:
  File "C:\Users\oriba\AppData\Local\Programs\Python\Python36\lib\encodings\cp1252.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x90 in position 97: character maps to <undefined>

在线搜索后,我发现将“encoding ='ISO-8859-1'”添加到'with open()'应该修复它...然后我收到此错误:

Traceback (most recent call last):
  File "C:/Users/oriba/Desktop/Month of Reddit/chatbot.py", line 40, in <module>
    row = json.load(row)
  File "C:\Users\oriba\AppData\Local\Programs\Python\Python36\lib\json\__init__.py", line 296, in load
BZh91AY&SYÔAÙÕÿî»ÿÿÿúÿÿÿÿÿÿÿÿc*è`  1Ï. ñÕ ¢U±Ã$'¤;\=@  ÝX9kl´ÜιKW; É@ Ò PQáGF  PÝ  Û   P      :è  
    return loads(fp.read(),
AttributeError: 'str' object has no attribute 'read'

现在我不知所措。我知道这很多,这对我来说很复杂。我感谢任何帮助:)

python json python-3.x
1个回答
0
投票

json.loads()完成这项工作。

loads()从File对象读取时,load()从str对象读取

你的代码是

for row in f:
    ...

row这里是一个str

© www.soinside.com 2019 - 2024. All rights reserved.