我正在尝试在不使用 pandas
的情况下在 python 中读取 excel 文件或 xlrd
,并且我一直在尝试将结果从 bytes
至utf-8
没有任何成功。
xls 文件中的数据
colA colB colC
spc 1D0 20190705
spd 1D0 20190705
spe 1D0 20190705
... (goes on for 500k lines)
代码
with open(file, 'rb') as f:
data = f.readlines(1) # Just to check the first line that is printed out
print(data[0].decode('utf-8'))
我收到的错误是
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd0 in position 0: invalid continuation byte
如果我要打印
data
不解码,结果为:[b'\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00>\x00\x03\x00\xfe\xff\t\x00\x06\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x9e\x00\x00\x00\x9dN\x00\x00\x00\x00\x00\x00\x00\x10\x00\x00\xfe\xff\xff\xff\x00\x00\x00\x00\xfeM\x00\x00\x01\x00\x00\x00\xffM\x00\x00\x00N\x00\x00\x01N\x00\x00\x02N\x00\x00\x03N\x00\x00\x04N\x00\x00\x05N\x00\x00\x06N\x00\x00\x07N\x00\x00\x08N\x00\x00\tN\x00\x00\n']
我没有任何理由不想使用
pandas
或 xlrd
,如果需要,我只是尝试使用标准库解析数据。有什么想法吗?
最佳答案
您需要先解压缩 xlsx 文件,然后才能读取其内容(假设这是您使用的格式)。
关于没有 Pandas 的Python解码excel表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56930653/