Libin Liu
Libin Liu

Reputation: 21

Does the encoding parameter work for pandas.read_excel?

I need to read .xls files by using pandas.read_excel. They are adsorption data directly exported from the software of the measurement equipment..I tried

pd.read_excel(r'./002-197.XLS',sheet_name=0, index_col=None,encoding='ISO-8859-1', na_values=['NA'])

But it shows:

*** No CODEPAGE record, no encoding_override: will use 'ascii' Traceback (most recent call last):

File "D:\PPy\data analysis\file_to_rdirectory.py", line 17, in exp_info=pd.read_excel(r'./002-197.XLS',sheet_name=0, index_col=None,encoding='ISO-8859-1', na_values=['NA'])

File "D:\Anaconda\envs\myenv\lib\site-packages\pandas\io\excel_base.py", line 304, in read_excel io = ExcelFile(io, engine=engine)

File "D:\Anaconda\envs\myenv\lib\site-packages\pandas\io\excel_base.py", line 824, in init self._reader = self._enginesengine

File "D:\Anaconda\envs\myenv\lib\site-packages\pandas\io\excel_xlrd.py", line 21, in init super().init(filepath_or_buffer)

File "D:\Anaconda\envs\myenv\lib\site-packages\pandas\io\excel_base.py", line 353, in init self.book = self.load_workbook(filepath_or_buffer)

File "D:\Anaconda\envs\myenv\lib\site-packages\pandas\io\excel_xlrd.py", line 36, in load_workbook return open_workbook(filepath_or_buffer)

File "D:\Anaconda\envs\myenv\lib\site-packages\xlrd__init__.py", line 148, in open_workbook bk = book.open_workbook_xls(

File "D:\Anaconda\envs\myenv\lib\site-packages\xlrd\book.py", line 108, in open_workbook_xls bk.fake_globals_get_sheet()

File "D:\Anaconda\envs\myenv\lib\site-packages\xlrd\book.py", line 732, in fake_globals_get_sheet self.get_sheets()

File "D:\Anaconda\envs\myenv\lib\site-packages\xlrd\book.py", line 723, in get_sheets self.get_sheet(sheetno)

File "D:\Anaconda\envs\myenv\lib\site-packages\xlrd\book.py", line 714, in get_sheet sh.read(self)

File "D:\Anaconda\envs\myenv\lib\site-packages\xlrd\sheet.py", line 1369, in read strg = unpack_string(data, 7, bk.encoding or bk.derive_encoding(), lenlen=1)

File "D:\Anaconda\envs\myenv\lib\site-packages\xlrd\biffh.py", line 250, in unpack_string return unicode(data[pos:pos+nchars], encoding)

File "D:\Anaconda\envs\myenv\lib\site-packages\xlrd\timemachine.py", line 31, in unicode = lambda b, enc: b.decode(enc)

UnicodeDecodeError: 'ascii' codec can't decode byte 0xb3 in position 10: ordinal not in range(128)

I tried to copy all the data from this excel file to a newly created excel file and it worked well. And I noticed that the first line in the error report.

*** No CODEPAGE record, no encoding_override: will use 'ascii' Traceback (most recent call last):

it seems the file doesn't have codepage record and can't be encoded by 'ascii'. So I tried to provide it by using encoding='' and encoding_override='' to the syntax, but no improvement. Can anyone help me?

Upvotes: 2

Views: 8726

Answers (1)

3olich
3olich

Reputation: 41

wb = xlrd.open_workbook(path, encoding_override='CORRECT_ENCODING')
df = pd.read_excel(wb)

Upvotes: 4

Related Questions