UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe0 in position 16: invalid continuation byte

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)