我試圖用Python讀入.xls文件。該文件包含多個非ASCII字符(即,äöü)。我嘗試過使用openpyxls和xlrd(我對xlrd寄予厚望,因爲它應該讀取unicode中的所有內容),但都沒有工作。從Python的xls讀取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
這紙條後立即只是試圖讀取文件出錯了
幷包括在頂部var ios版本:
# -*- coding: utf-8 -*-
我在Windows Server 2008機器上的python 2.7上運行此操作。