Using XLRD in Ironpython

自闭症网瘾萝莉.ら 提交于 2020-01-04 10:00:08

问题


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.



来源:https://stackoverflow.com/questions/16573834/using-xlrd-in-ironpython

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!