本文介绍了从网上下载excel文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 嗨 - 有经验的程序员,但这是我的第一个Python程序。 此URL将检索包含(当天)的excel电子表格 msci股票指数回归。 http://www.mscibarra.com/webapp/inde...EIPerfRegional 想编写python来下载并保存文件。 到目前为止,我已经达到了这个目的: 解决方案 7月28日,3:00 * pm,p ... @ well.com < p ... @ well.comwrote: Woops在我想要预览时点击发送。看起来像html [quote]标签 无法使用groups.google.com(不错)。 Anway,在上面的测试1中,我决定如何实例化一个excel 对象;把一些东西放进去;然后保存到磁盘。 所以,从理论上讲,我正在检索我的Excel电子表格 response = urllib2.urlopen() 除了我对此做了什么? 好好阅读一些urllib2文档并找到 请求类使用方法has_data()。它返回False。 嗯,这不令人鼓舞。 我想要了解urllib2.urlopen返回的技巧 给我;在那里翻找;并希望找到我的excel文件。 我使用pdb进行调试。这很有意思: (Pdb)目录(回复) [''__doc__'',''_ _ init _''''''__ item__'', ''__module__'',''_ _ jpr'''''''关闭'', ''代码'','' fileno'',''fp' ',''geturl'',''header'',''info'',''msg'',''next'',''read'', ''readline' ','' readlines'',''url''] (Pdb) 我想成员们__ * _是方法;没有 下划线的名字是属性(变量)(?)。 或者这可能不是正确的方向(也许那里 是做这个东西的更好的模块)。很高兴知道如果是这样的话(如果能为我完成工作的话)( )。 pat Woops hit我想要预览时发送。看起来像html 不,这些是所有属性和方法的名称。 read是一种方法,例如。 文档( http://docs.python.org/lib/module-urllib2.html 非常好 清楚: """ 此函数返回一个类似文件的对象,有两种方法: """ 然后是类似文件的对象: http://docs.python.org/lib/bltin-file-objects.html """ read([ size]) 从文件中读取最多大小字节(如果读取在获取大小字节之前达到EOF ,则更少)。如果size参数为负数或 省略,则读取所有数据,直到达到EOF。字节作为 字符串对象返回。遇到EOF时会返回一个空字符串 。 (对于某些文件,比如ttys,在EOF被击中后继续 读取是有意义的。)注意这个方法可能会调用 底层C函数fread()不止一次努力获得尽可能接近大小字节的。另请注意,当处于非阻塞状态时,即使没有给出大小 参数,也可以返回比请求数据少的数据。 "" Diez 不,这些是所有属性和方法的名称。 read是一种方法,例如。 对 - 我倒退了。 文档( http://docs.python.org/lib/module-urllib2.html 非常好 清楚: """ 此函数返回一个类似文件的对象,其中包含两种方法: """ 然后是类似文件的对象: http://docs.python.org/lib/bltin-file-objects.html 读取(* [size]) * * *从文件中读取大多数大小字节(如果读取到达EOF ,则在获取大小字节之前减去)。如果size参数为负数或 省略,则读取所有数据,直到达到EOF。字节作为 字符串对象返回。遇到EOF时会返回一个空字符串 。 (对于某些文件,比如ttys,在EOF被击中后继续 读取是有意义的。)注意这个方法可能会调用 底层C函数fread()不止一次努力获得尽可能接近大小字节的。另请注意,当处于非阻塞状态时,即使没有给出大小 参数,也可以返回比请求数据少的数据。 """ Diez 偶然发现.read: response = urllib2.urlopen('''http://www.mscibarra.com/webapp/indexperf/ excel? priceLevel = 0& scope = 0& currency = 15& style = C& size = 36& m arket = 1897& asOf = Jul + 25%2C + 2008& export = Excel_IEIPerfRegional'')。阅读 现在问题是:该怎么办?我会查看您指向的 文档。 thanx - pat Hi - experienced programmer but this is my first Python program.This URL will retrieve an excel spreadsheet containing (that day''s)msci stock index returns. http://www.mscibarra.com/webapp/inde...EIPerfRegionalWant to write python to download and save the file.So far I''ve arrived at this: 解决方案Woops hit Send when I wanted Preview. Looks like the html [quote] tagdoesn''t work from groups.google.com (nice).Anway, in test 1 above, I determined how to instantiate an excelobject; put some stuff in it; then save to disk.So, in theory, I''m retrieving my excel spreadsheet withresponse = urllib2.urlopen()Except what then do I do with this?Well for one read some of the urllib2 documentation and found theRequest class with the method has_data() on it. It returns False.Hmm that''s not encouraging.I supposed the trick to understand what urllib2.urlopen is returningto me; rummage around in there; and hopefully find my excel file.I use pdb to debug. This is interesting:(Pdb) dir(response)[''__doc__'', ''__init__'', ''__iter__'', ''__module__'', ''__repr__'', ''close'',''code'', ''fileno'', ''fp'', ''geturl'', ''headers'', ''info'', ''msg'', ''next'', ''read'',''readline'', ''readlines'', ''url''](Pdb)I suppose the members with __*_ are methods; and the names without theunderbars are attributes (variables) (?).Or maybe this isn''t at all the right direction to take (maybe thereare much better modules to do this stuff). Would be happy to learn ifthat''s the case (and if that gets the job done for me).patWoops hit Send when I wanted Preview. Looks like the htmlNo, these are the names of all attributes and methods. read is a method,for example.The docs (http://docs.python.org/lib/module-urllib2.html) are prettyclear on this:"""This function returns a file-like object with two additional methods:"""And then for file-like objects: http://docs.python.org/lib/bltin-file-objects.html"""read( [size])Read at most size bytes from the file (less if the read hits EOFbefore obtaining size bytes). If the size argument is negative oromitted, read all data until EOF is reached. The bytes are returned as astring object. An empty string is returned when EOF is encounteredimmediately. (For certain files, like ttys, it makes sense to continuereading after an EOF is hit.) Note that this method may call theunderlying C function fread() more than once in an effort to acquire asclose to size bytes as possible. Also note that when in non-blockingmode, less data than what was requested may be returned, even if no sizeparameter was given."""DiezNo, these are the names of all attributes and methods. read is a method,for example.right - I got it backwards.The docs (http://docs.python.org/lib/module-urllib2.html) are prettyclear on this:"""This function returns a file-like object with two additional methods:"""And then for file-like objects: http://docs.python.org/lib/bltin-file-objects.html"""read( * [size])* * *Read at most size bytes from the file (less if the read hits EOFbefore obtaining size bytes). If the size argument is negative oromitted, read all data until EOF is reached. The bytes are returned as astring object. An empty string is returned when EOF is encounteredimmediately. (For certain files, like ttys, it makes sense to continuereading after an EOF is hit.) Note that this method may call theunderlying C function fread() more than once in an effort to acquire asclose to size bytes as possible. Also note that when in non-blockingmode, less data than what was requested may be returned, even if no sizeparameter was given."""DiezJust stumbled upon .read:response = urllib2.urlopen(''http://www.mscibarra.com/webapp/indexperf/excel?priceLevel=0&scope=0¤cy=15&style=C&size=36&m arket=1897&asOf=Jul+25%2C+2008&export=Excel_IEIPerfRegional'').readNow the question is: what to do with this? I''ll look at thedocumentation that you point to.thanx - pat 这篇关于从网上下载excel文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-20 07:28