Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用Pandas从URL读取excel文件-XLRDError_Python_Pandas_Xlrd - Fatal编程技术网

Python 使用Pandas从URL读取excel文件-XLRDError

Python 使用Pandas从URL读取excel文件-XLRDError,python,pandas,xlrd,Python,Pandas,Xlrd,我正在尝试从以下URL将excel文件读入熊猫: url1 = 'https://cib.societegenerale.com/fileadmin/indices_feeds/CTA_Historical.xls' url2 = 'https://cib.societegenerale.com/fileadmin/indices_feeds/STTI_Historical.xls' 使用代码: pd.read_excel(url1) 但是它不起作用,我得到了错误: XLRDError:

我正在尝试从以下URL将excel文件读入熊猫:

url1 = 'https://cib.societegenerale.com/fileadmin/indices_feeds/CTA_Historical.xls'

url2 = 'https://cib.societegenerale.com/fileadmin/indices_feeds/STTI_Historical.xls'
使用代码:

pd.read_excel(url1)
但是它不起作用,我得到了错误:

XLRDError: Unsupported format, or corrupt file: Expected BOF record; found '2000/01/'
在谷歌上搜索后,似乎有时通过URL提供的.xls文件实际上在幕后以不同的文件格式保存,如html或xml

当我手动下载excel文件并使用excel打开它时,会收到一条错误消息:文件格式和扩展名不匹配。文件可能已损坏或不安全。除非你相信它的来源,否则不要打开它“

当我打开它时,它看起来就像一个普通的excel文件

我在网上看到一篇帖子,建议我在文本编辑器中打开文件,看看是否有关于正确文件格式的其他信息,但在使用notepad++打开时,我看不到任何其他信息

有人能帮我把这个“xls”文件正确地读入pandas DataFramj吗?

看来你可以使用:

然后我检查最后一列
f
是否有其他值,如
NaN

print df[df.f.notnull()]

Empty DataFrame
Columns: [a, b, c, d, e, f]
Index: []
因此只有
NaN
,所以您可以通过参数
usecols
过滤最后一列
f

import pandas as pd

df = pd.read_csv('https://cib.societegenerale.com/fileadmin/indices_feeds/CTA_Historical.xls',
                 sep='\t',
                 parse_dates=[0],
                 names=['a','b','c','d','e','f'],
                 usecols=['a','b','c','d','e'])
print df

如果这对某人有帮助..你可以直接通过Excel中的URL读取Google驱动器文件,而无需任何登录要求。我在Google Colab中尝试过,它很有效

  • 将XL文件上载到Google Drive,或使用已上载的文件
  • 通过链接将文件共享给任何人(我不知道是否仅查看有效,但我尝试了完全访问)
  • 复制链接
你会得到这样的东西

共享url:
https://drive.google.com/file/d/---some--long--string/view?usp=sharing

从尝试下载文件中获取下载url(从那里复制url)

它将是这样的:(它拥有与上面相同的谷歌文件id)

下载网址:
https://drive.google.com/u/0/uc?id=---some--long--string&export=download

现在转到Google Colab并粘贴以下代码:

import pandas as pd

fileurl   = r'https://drive.google.com/file/d/---some--long--string/view?usp=sharing'
filedlurl = r'https://drive.google.com/u/0/uc?id=---some--long--string&export=download'

df = pd.read_excel(filedlurl)
df

就是这样。文件在你的df中。

啊,太棒了,谢谢!工作得很好!-你知道read\u csv会工作吗?或者有什么方法可以告诉你吗?首先,当我用文件打开url时,Excel返回警告。然后我用
记事本+
检查文件,它看起来像
csv
。所以我使用read\u csv,效果非常好。G祝你好运!谢谢你提供的信息-我也用记事本打开了它试图查看,但是你在哪里看到了它是csv的附加信息?我刚刚看到了其中包含的文本数据。对不起,它是txt。没有csv。但是read_csv经常阅读一些结构良好的txt非常好。谢谢你接受。谢谢你的帮助-非常感谢!
import pandas as pd

fileurl   = r'https://drive.google.com/file/d/---some--long--string/view?usp=sharing'
filedlurl = r'https://drive.google.com/u/0/uc?id=---some--long--string&export=download'

df = pd.read_excel(filedlurl)
df