UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe0 in position 16: invalid continuation byte
When I run the program part below, I encountered the error responses above, the main reason for that error is that the file content contains incorrect codecs character. Sometimes it is hard to dig out where the bad character is. One suggestion is to modify 2 lines in function resort_ctags() in ctags.py to ignore or replace the bad character. For example, to replace the errors with some standard python character, add errors=‘replace’ to the following two lines in function resort_ctags(). See the following example.
#!/usr/bin/env python
# -*- coding:utf-8 -*-
with open(file_name, 'r', encoding='utf-8') as data:
for line in data.readlines():
print(line)
- change to the following way
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import codecs
with open(file_name, 'r', encoding='utf-8', errors='replace') as data:
for line in data.readlines():
print(line)
My UnicodeDecodeError are gone after manually doing the above.
https://github.com/SublimeText/CTags/issues/194
https://docs.python.org/2/library/codecs.html#codec-base-classes
本文介绍了解决Python中UnicodeDecodeError的方法,主要原因是文件中含有不正确的字符编码。通过修改读取文件时的错误处理方式,使用'replace'参数来替换无法解码的字符,可以有效避免这一错误。
2965





