python下不知道什么时候就会报编码错误,走一波,试试效果如何。
[Python] 纯文本查看 复制代码
import os
import codecs
# 读取文本文件函数,支持bom-utf-8,utf-8,utf-16,gbk,gb2312
# 返回文件内容
def ReadTextFile(filepath):
try:
file = open(filepath, 'rb')
except IOError as err:
print('读取文件出错 in ReadFile', err)
bytes = file.read()
file.close()
if bytes[:3] == codecs.BOM_UTF8:
content = bytes[3:].decode('utf-8')
else:
try:
content = bytes.decode('gb2312')
except UnicodeDecodeError as err:
try:
content = bytes.decode('utf-16')
except UnicodeDecodeError as err:
try:
content = bytes.decode('utf-8')
except UnicodeDecodeError as err:
try:
content = bytes.decode('gbk')
except UnicodeDecodeError as err:
content = ''
print('不支持此种类型的文本文件编码', err)
return content
|