读取使用urllib2检索的Excel对象 [英] Reading an Excel object retrieved using urllib2

查看:134
本文介绍了读取使用urllib2检索的Excel对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用urllib2获取一个Excel文件,并保存到下面的响应中。我想要使​​用xlrd或类似的方式处理这个excel文件。我在下面列出了一些信息,让我知道如果我能提供更多的信息。如何将响应对象转换成可以播放的对象?

  response =< addinfourl at 199999998 which fp =< ; socket._fileobject对象在0x100001010>> 

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

标题:
内容类型:应用程序/ vnd.ms-excel
传输编码:chunked


解决方案

使用 xlrd ,并根据它的API文档,看起来你可以使用类似的东西:

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

似乎不支持阅读文件对象(其中,IMO,将是理想的),只接受 filename 本身或上述 file_contents 方法。



如果 file_contents 不存在或不起作用'必须使用 tempfile 将响应写入临时文件并读取/ p>

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

解决方案

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.

这篇关于读取使用urllib2检索的Excel对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆