我正在使用一些代码,这些代码使用pandas dataframe.to_excel()写入.xlsx,然后使用pandas.read_excel()读取它们。
我遇到的问题是,尽管仅使用UTF-8字符生成文件并强制使用UTF-8编码,但我收到“ UnicodeDecodeError:'charmap'编解码器无法解码位置70:字符的字节0x8f尝试读取数据时映射为“。
这是我制作文件的方式-看来工作正常,并且文件在excel / normal中可读:
with pandas.ExcelWriter(path, engine='xlsxwriter', options={'strings_to_numbers': True, 'encoding':'utf-8'}) as writer:
dataframe = pandas.DataFrame(data)
dataframe.to_excel(writer, sheet_name, header=False, index=False)
这是我尝试回读数据的方式:
data = pandas.read_excel(f, sheets, header=False, dtype='str', encoding='UTF-8')
“数据”只是一个二维numpy字符串数组,仅包含您的标准键盘字符,没有UTF-8不友好的怪异之处。一些字符串为空(“”)。我在作家和阅读器上使用编码选项-没有理由应该有任何不寻常的字符。
我很茫然-这不应该是pandas.read_excel的确切预期用例吗?任何帮助将不胜感激。