我正在尝试使用 Python 读取 .xls 文件。该文件包含多个非 ascii 字符(即 äöü)。我已经尝试过使用 openpyxls 和 xlrd(我对 xlrd 寄予厚望,因为它应该以 unicode 格式读取所有内容),但都没有工作。
在尝试从 xls 打印信息时,我发现了多个处理编码/解码的答案,但我什至似乎都无法做到这一点。此脚本在尝试读取文件后立即出错:
import xlrd
workbook = xlrd.open_workbook('export_data.xls')
导致:
Traceback (most recent call last):
File "C:\Users\Administrator\workspace\tufinderxlstoxml\tufinderxlstoxml2.py", line 2, in <module>
workbook = xlrd.open_workbook('export_data.xls')
File "C:\Python27_32\lib\site-packages\xlrd\__init__.py", line 435, in open_workbook
ragged_rows=ragged_rows,
File "C:\Python27_32\lib\site-packages\xlrd\book.py", line 119, in open_workbook_xls
bk.get_sheets()
File "C:\Python27_32\lib\site-packages\xlrd\book.py", line 705, in get_sheets
self.get_sheet(sheetno)
File "C:\Python27_32\lib\site-packages\xlrd\book.py", line 696, in get_sheet
sh.read(self)
File "C:\Python27_32\lib\site-packages\xlrd\sheet.py", line 796, in read
strg = unpack_string(data, 6, bk.encoding or bk.derive_encoding(), lenlen=2)
File "C:\Python27_32\lib\site-packages\xlrd\biffh.py", line 269, in unpack_string
return unicode(data[pos:pos+nchars], encoding)
UnicodeDecodeError: 'ascii' codec can't decode byte 0x92 in position 55: ordinal not in range(128)
WARNING *** OLE2 inconsistency: SSCS size is 0 but SSAT size is non-zero
*** No CODEPAGE record, no encoding_override: will use 'ascii'
*** No CODEPAGE record, no encoding_override: will use 'ascii'
我也试过:
workbook = xlrd.open_workbook('export_data.xls', encoding_override="utf-8")
导致:
Traceback (most recent call last):
File "C:\Users\Administrator\workspace\tufinderxlstoxml\tufinderxlstoxml2.py", line 2, in <module>
workbook = xlrd.open_workbook('export_data.xls', encoding_override="utf-8")
File "C:\Python27_32\lib\site-packages\xlrd\__init__.py", line 435, in open_workbook
ragged_rows=ragged_rows,
File "C:\Python27_32\lib\site-packages\xlrd\book.py", line 119, in open_workbook_xls
bk.get_sheets()
File "C:\Python27_32\lib\site-packages\xlrd\book.py", line 705, in get_sheets
self.get_sheet(sheetno)
File "C:\Python27_32\lib\site-packages\xlrd\book.py", line 696, in get_sheet
sh.read(self)
File "C:\Python27_32\lib\site-packages\xlrd\sheet.py", line 796, in read
strg = unpack_string(data, 6, bk.encoding or bk.derive_encoding(), lenlen=2)
File "C:\Python27_32\lib\site-packages\xlrd\biffh.py", line 269, in unpack_string
return unicode(data[pos:pos+nchars], encoding)
UnicodeDecodeError: 'utf8' codec can't decode byte 0x92 in position 55: invalid start byte
WARNING *** OLE2 inconsistency: SSCS size is 0 but SSAT size is non-zero
并在顶部包括以下各种版本:
# -*- coding: utf-8 -*-
我在 Windows Server 2008 机器上的 python 2.7 上运行它。