python - Python/JSON:一个接一个的错误
问题描述
我正在关注制作聊天机器人的在线教程......这是开始部分的代码
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>
在网上搜索后,我发现在'with open()'中添加“encoding='ISO-8859-1'”应该可以解决它......然后我得到这个错误:
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'
而现在我不知所措。我知道这很多,这对我来说很复杂。我很感激任何帮助:)
解决方案
json.loads()
做这项工作。
loads()
load()
在从 File 对象读取时从 str 对象读取
你的代码是
for row in f:
...
row
这是一个str
推荐阅读
- vue.js - 如何解决避免改变道具
- python - Python Statsmodel 中的 RLM 残差
- python - 需要有关获取实时货运跟踪信息到 magento 的建议
- google-cloud-platform - 使用 Dataflow 将 csv 数据从 Pub/Sub 订阅流式传输到 BigQuery
- c++ - 临时约束需要需要需要需要吗?
- azure - 扩展包大小超过最大包大小限制
- azure - 从 Azure Function 触发 UIPath 机器人
- postgresql - 在一天中的特定时间发送 grafana 警报
- javascript - 无法使用 pdf.js 从 pdf 中提取 jpg
- javascript - 再次渲染被阻止的脚本 type="text/plain"