Reading an Excel object retrieved using urllib2

2019-02-23 17:52发布

I am getting an Excel file using urllib2 and saving into response below. I want to be able to process this excel file using xlrd or similar. I included some info below, let me know if I can provide more info. How can I have response object transformed into an object I can play with?

response = <addinfourl at 199999998 whose fp = <socket._fileobject object at 0x100001010>>

response.read() prints: '\xd0\xcf\x11\xe0...'

Headers:
Content-Type: application/vnd.ms-excel
Transfer-Encoding: chunked

1条回答
趁早两清
2楼-- · 2019-02-23 18:14

Using xlrd, and based on its API documentation, it appears like you can use something similar to this:

book = xlrd.open_workbook(file_contents=response.read())

It doesn't appear to support reading a file object (which, IMO, would be ideal), only taking in a filename itself or the above file_contents method.

If file_contents didn't exist or didn't work, you'd have to use tempfile to write the response to a temporary file and read that.

查看更多
登录 后发表回答