Python解码没有pandas的excel表 [英] Python decoding excel sheet without pandas

查看:29
本文介绍了Python解码没有pandas的excel表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在不使用 pandasxlrd 的情况下在 python 中读取 excel 文件,并且我一直在尝试从 bytes 转换结果> 到 utf-8 没有任何成功.

I am trying to read an excel file in python without using pandas or xlrd, and I have been trying to convert the results from bytes to utf-8 without any success.

来自 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\x00\x00\x00\x00xff\xff\xff\x00\x00\x00\x00\xfeM\x00\x00\x01\x00\x00\x00\xffM\x00\x00\x00N\x00\x00\x01N\x00\x00\x02N\x0x00\x03N\x00\x00\x04N\x00\x00\x05N\x00\x00\x06N\x00\x00\x07N\x00\x00\x08N\x00\x00\tN\x00\x00\n']

If I were to print data without decoding it, the result is: [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']

我没有任何理由不想使用 pandasxlrd,我只是想在需要时仅使用标准库来解析数据.

There isn't any reason why I don't want to use pandas or xlrd, I am just trying to parse the data with just the standard libraries if required.

有什么想法吗?

推荐答案

您需要先解压 xlsx 文件,然后才能读取其内容(假设您使用的是这种格式).

You need to unzip the xlsx file first, before you can read its contents (assuming that is the format you are using).

这篇关于Python解码没有pandas的excel表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆