我正在尝试使用 xlrd 打开一个 Excel 文件 (.xls(。这是我正在使用的代码的摘要:
def import_excel(key):
key_url = os.path.join(UPLOAD_DIR, key)
data = xlrd.open_workbook(key_url)
table = data.sheets()[0]
nrows = table.nrows
ncols = table.ncols
colnames = table.row_values(0)
list = []
for rownum in range(1, nrows):
row = table.row_values(rownum)
if row:
app = {}
for i in range(len(colnames)):
app[colnames[i]] = row[i]
list.append(app)
return list
这适用于大多数文件,但对于我从特定组织获取的文件失败。当我尝试从该组织打开Excel文件时,我遇到的错误如下。
ERROR *** codepage 10008 -> encoding 'unknown_codepage_10008' -> LookupError: unknown encoding: unknown_codepage_10008
Internal Server Error: /admin/result/detail/import/
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py", line 149, in get_response
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py", line 149, in get_response
response = self.process_exception_by_middleware(e, request)
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py", line 147, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Library/Python/2.7/site-packages/django/utils/decorators.py", line 184, in inner
return func(*args, **kwargs)
File "/Library/Python/2.7/site-packages/django/contrib/auth/decorators.py", line 23, in _wrapped_view
return view_func(request, *args, **kwargs)
File "/Users/attackt/Desktop/jinji_workspace/grape/web/views/result_view.py", line 184, in detail_import
item_list = import_excel(key)
File "/Users/attackt/Desktop/jinji_workspace/grape/utils.py", line 45, in import_excel
data = xlrd.open_workbook(key_url)
File "/Library/Python/2.7/site-packages/xlrd/__init__.py", line 441, in open_workbook
ragged_rows=ragged_rows,
File "/Library/Python/2.7/site-packages/xlrd/book.py", line 116, in open_workbook_xls
bk.parse_globals()
File "/Library/Python/2.7/site-packages/xlrd/book.py", line 1170, in parse_globals
self.handle_codepage(data)
File "/Library/Python/2.7/site-packages/xlrd/book.py", line 794, in handle_codepage
self.derive_encoding()
File "/Library/Python/2.7/site-packages/xlrd/book.py", line 775, in derive_encoding
_unused = unicode(b'trial', self.encoding)
LookupError: unknown encoding: unknown_codepage_10008
在此处输入链接说明
链接是一个错误文件 xls
例如:。
abook = xlrd.open_workbook(..., encoding_override="cp10008")