Using XLRD in Ironpython

2019-02-26 20:13发布

问题:

I am trying to use the XLRD library in IronPython 2.7

At the most basic operation of opening an .xls file (2003 format) I get the following error, and I am not sure how to fix it:

  workbook = xlrd.open_workbook(xlsfile)

  File "C:\Program Files (x86)\IronPython 2.7\lib\site-packages\xlrd\__init__.py", line    426, in open_workbook
  TypeError: sequence item 0: expected bytes or byte array, str found

any ideas? i would like to use xlrd if possible but seems like there may be some compatibility problems. When using Python 2.7 interpreter the file opens no problem ..

回答1:

I fought with this for some time in xlrd 0.9.2. Neither bytes, bytearray, str, or even reading the file manually and passing it as file_contents worked.

Finally I downgraded xlrd to version 0.8.0 and... it's working. :)



回答2:

For whatever reason it seems that open_workbook requires the filename to be in bytes, not as a string. Try:

workbook = xlrd.open_workbook(bytes(xlsfile))

That's an issue you're likely to encounter a lot: IronPython's strings are Unicode by default (like Python 3) and not byte strings like Python 2.