在读取多个txt文件时,python会报编码异常,主要是你无法判断当前txt是哪种类型的编码
参考下面程序
def get_encoding(filename): #二进制读取方式,获取字节数据, 检测编码类型
with open(filename,'rb') as f:
return chardet.detect(f.read())['encoding']
def file_read(filename, file_code): #以文件编码类型读取文件
print(filename)
with open(filename, 'r', encoding=file_code, errors="ignore") as file_to_read:
file_content = file_to_read.read()
return file_content