python xlrd 未知编码:unknown_codepage_10008
python xlrd unknown encoding: unknown_codepage_10008
我正在尝试使用 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
Link 是一个错误文件 xls
使用举例:。
abook = xlrd.open_workbook(..., encoding_override="cp10008")
我正在尝试使用 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
Link 是一个错误文件 xls
使用举例:。
abook = xlrd.open_workbook(..., encoding_override="cp10008")