import chardet
class File_class:
def __init__(self, path):
self.path = path
def open_file(self):
# 读取文件
# with open(self.path, 'rb') as f1:
# result = chardet.detect(f1.read())
# encoding = result['encoding']
# print(result)
with open(self.path, "r+") as f:
lines = f.readlines()
print(lines)
# 判断文件类型,切分每一行
# excle文件,4个空格
for line in lines:
print(line)
path = r"C:\Users\Administrator\Desktop\data\计算机产品价格.xlsx"
f = File_class(path)
f.open_file()
使用with open 方法读取一个excel文件一直报编码错误,gbk和utf-8都试用了,也是不行
报错信息:UnicodeDecodeError: 'gbk' codec can't decode byte 0xfe in position 53: illegal multibyte sequence
使用chardet.detect(f1.read())查看编码信息返回None