解决Python报错UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 658: illegal multibyte

解决Python报错–UnicodeDecodeError: ‘gbk’ codec can’t decode byte 0x80 in position 658: illegal multibyte sequence

今天在anaconda上新建一个python3.6环境后,使用时报错了:

(base) C:\Users\peter>activate tf
(tf) C:\Users\peter>python
Python 3.6.7 (default, Feb 28 2019, 07:28:18) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Failed calling sys.__interactivehook__
Traceback (most recent call last):
  File "C:\Users\peter\AppData\Local\Continuum\anaconda3\envs\tf\lib\site.py", line 410, in register_readline
    readline.read_history_file(history)
  File "C:\Users\peter\AppData\Local\Continuum\anaconda3\envs\tf\lib\site-packages\pyreadline\rlmain.py", line 165, in read_history_file
    self.mode._history.read_history_file(filename)
  File "C:\Users\peter\AppData\Local\Continuum\anaconda3\envs\tf\lib\site-packages\pyreadline\lineeditor\history.py", line 82, in read_history_file
    for line in open(filename, 'r'):
UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 658: illegal multibyte sequence

网上查阅资料后发现这是win10下特有的问题


解决方案如下:
打开报错的倒数第三行的history.py文件,定位到82行,源代码如下:
在这里插入图片描述
添加代码

,encoding='utf-8'

在这里插入图片描述
再次运行Python就不会报错了。

发布了542 篇原创文章 · 获赞 238 · 访问量 75万+
展开阅读全文

UnicodeDecodeError: 'gbk' codec can't decode byte

11-11

运行 "Learn Python the Hard Way" 里的代码时出现的报错。代码: ``` 1. from sys import argv 2. from os.path import exists 3. 4. script, from_file, to_file = argv 5. 6. print(f"Copying from {from_file} to {to_file}") 7. 8. # we could do these two on one line, how? 9. in_file = open(from_file) 10. indata = in_file.read() 11. 12. print(f"The input file is {len(indata)} bytes long") 13. 14. print(f"Does the output file exsit? {exists(to_file)}") 15. print("Ready, hit RETURN to continue, CTRL-C to abort.") 16. input() 17. 18. out_file = open(to_file, 'w') 19. out_file.write(indata) 20. 21. print("Alright, all done.") 22. 23. out_file.close() 24. in_file.close() ``` 照抄书本在 Terminal 上输入的命令: $ echo "This is a test file." > test.txt $ cat test.txt This is a test file. $ python ex17.py test.txt new_file.txt 下面是 Terminal 给的提示: Copying from test.txt to new_file.txt Traback ( most recent call last): File "ex17.py", line 10, in <module> indata = in_file.read() UnicodeDecodeError: 'gbk' codec can't decode byte 0xff in position 0: illegal multibyte sequence 尝试把 'gbk' 编码改为 'UTF-8': 9. in_file = open(from_file, 'r', encoding = 'UTF-8') Terminal 给出的提示 Copying from test.txt to new_file.txt Traceback (most recent call last): File "ex17.py", line 10, in <Module> indata = in_file.read() File "C:\Python36\lib\codecs.py", line 321, in decode (result, consumed) = self._buffer_decode(data, self.errors, final) UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte 求解:是什么导致这个 Error ?以及如何解决? 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览