selfteaching / selfteaching/selfteaching-python-camp

Day09 作业中读取tang300.json文件出错的解决方法

Open
#1,142 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

学习心得分享
Dominant language
Python
Stars
151
Forks
875
PR merge metrics
No merged PRs in 30d

Description

错误代码如下:
UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 73: illegal multibyte sequence

出现这个问题可能是因为文件不是UTF8编码的,所以改为对应的编码方式即可。

#原语句
with open('tang300.json') as f:
    text = f.read()
f.closed

#修改后
with open('tang300.json','r',encoding='UTF-8') as f:
    text = f.read()
f.closed

(网上有说修改读取方式也行:with open('tang300.json','rd') as f: ,但是我试了不行,大家好奇可以试试。)

这样就好了,如果词频结果出现了英文,可以把英文限制为输出0个解决。也可以更改词频代码。

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Day09 exercise and the referenced tang300.json file, then reproduce the UnicodeDecodeError from the original open call. Check how the exercise currently explains file encoding and update the relevant guidance so the documented fix and expected word-frequency result are clear.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
documentation
Issue type
Bug
Difficulty
1/5
Estimated time
Under an hour
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.