2

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?

2
  • df= pd.read_csv('Your.csv', encoding='ISO-8859-1') try changing with df= pd.read_csv('Your.csv', encoding='latin1')CommentedJun 12, 2020 at 14:48
  • 1
    Thank you. But I need to read excel not .csv.
    – Libin Liu
    CommentedJun 15, 2020 at 1:45

1 Answer 1

4
wb = xlrd.open_workbook(path, encoding_override='CORRECT_ENCODING') df = pd.read_excel(wb) 
2
  • This one throws an error TypeError: expected str, bytes or os.PathLike object, not Book .
    – Kokokoko
    CommentedMar 10, 2021 at 18:35
  • It worked okay for me. pd.read_excel() works fine with xlrd.Book as it is commented in the official documentation
    – ChesuCR
    CommentedOct 18, 2022 at 11:48

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.