简体   繁体   中英

Python Error when reading data from .xls file

I need to read a few xls files into Python.The sample data file can be found through Link: data.file . I tried:

import pandas as pd
pd.read_excel('data.xls',sheet=1)

But it gives an error message:

ERROR *** codepage 21010 -> encoding 'unknown_codepage_21010' -> LookupError: unknown encoding: unknown_codepage_21010 Traceback (most recent call last):

File "", line 1, in pd.read_excel('data.xls',sheet=1)

File "C:\\Anaconda3\\lib\\site-packages\\pandas\\io\\excel.py", line 113, in read_excel return ExcelFile(io, engine=engine).parse(sheetname=sheetname, **kwds)

File "C:\\Anaconda3\\lib\\site-packages\\pandas\\io\\excel.py", line 150, in init self.book = xlrd.open_workbook(io)

File "C:\\Anaconda3\\lib\\site-packages\\xlrd__init__.py", line 435, in open_workbook ragged_rows=ragged_rows,

File "C:\\Anaconda3\\lib\\site-packages\\xlrd\\book.py", line 116, in open_workbook_xls bk.parse_globals()

File "C:\\Anaconda3\\lib\\site-packages\\xlrd\\book.py", line 1170, in parse_globals self.handle_codepage(data)

File "C:\\Anaconda3\\lib\\site-packages\\xlrd\\book.py", line 794, in handle_codepage self.derive_encoding()

File "C:\\Anaconda3\\lib\\site-packages\\xlrd\\book.py", line 775, in derive_encoding _unused = unicode(b'trial', self.encoding)

File "C:\\Anaconda3\\lib\\site-packages\\xlrd\\timemachine.py", line 30, in unicode = lambda b, enc: b.decode(enc)

LookupError: unknown encoding: unknown_codepage_21010

Anyone could help with this problem?

PS: I know if I open the file in windows excel, and resave it, the code could work, but I am looking for a solution without manual adjustment.

using the ExcelFile class, I was successfully able to read the file into python. let me know if this helps!

    import xlrd
    import pandas as pd

    xls = pd.ExcelFile(’C:\data.xls’)
    xls.parse(’Index Constituents Data’, index_col=None, na_values=[’NA’])

The below worked for me.

import xlrd


my_xls = xlrd.open_workbook('//myshareddrive/something/test.xls',encoding_override="gb2312")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM