gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 00:50:53 25 4
gpt4 key购买 nike

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

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/'

在 Google 上搜索后,似乎有时通过 URL 提供的 .xls 文件实际上在幕后以不同的文件格式保存,例如 html 或 xml。

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

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

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

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

最佳答案

看来你可以使用read_csv :

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'])
print df

然后我检查最后一列 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

关于python - 使用 Pandas 从 URL 读取 excel 文件 - XLRDError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37243121/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com