如何修复Python中的JSON UnicodeDecodeError:'utf8'错误?

2024-04-26 05:37:18 发布

您现在位置:Python中文网/ 问答频道 /正文

在尝试读取名为“RC_2015-01”的JSON文件时,我收到一个解码错误。问题出现在摘录的第8行“对于f中的行:”

JSON文件没有收到其他用户报告的问题,因此我认为这是我的代码的问题

我使用Python3.7和pycharm作为我的IDE

我曾尝试向open函数添加多个encoding=“example”参数,但没有成功

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

    with open('RC_{}.bz2'.format(timeframe), buffering=1000) as f:
        print(f)
        for row in f:
            print(row)
            row_counter += 1
            row = json.loads(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']

Tags: 文件nameidjsonformatcounterbodyopen