简体   繁体   English

从 python 中的 URL 读取 xls 文件

[英]Read xls file from a URL in python

I'm trying to read data from the link below in python https://drive.google.com/file/d/16cp23cJxeyUfnBHMp-sNCuFNQxe8cqOV/view我正在尝试从 python https://drive.google.com/file/d/16cp23cJxeyUfnBHMp-sNCuFNQxe8cqOV/view中的链接读取数据

I've tried this:我试过这个:

import pandas as pd

path = pd.read_excel('https://drive.google.com/file/d/16cp23cJxeyUfnBHMp-sNCuFNQxe8cqOV/view')

That returned this error:这返回了这个错误:

XLRDError: Unsupported format, or corrupt file: Expected BOF record; XLRDError:不支持的格式,或损坏的文件:预期的 BOF 记录; found b'<!DOCTYP'找到 b'<!DOCTYP'

Then I tried using cvs format然后我尝试使用 cvs 格式

path = pd.read_csv('https://drive.google.com/file/d/16cp23cJxeyUfnBHMp-sNCuFNQxe8cqOV/view')

Returned this退了这个

ParserError: Error tokenizing data. ParserError:错误标记数据。 C error: Expected 298 fields in line 133, saw 440 C 错误:预计第 133 行中有 298 个字段,看到 440

finnaly I tried this:最后我试过这个:

path = pd.read_csv("https://drive.google.com/file/d/16cp23cJxeyUfnBHMp-sNCuFNQxe8cqOV/view")

This read data but that is not what I expected after seen the link (283 rows, 7 columns).此读取数据,但这不是我在看到链接后所期望的(283 行,7 列)。 Photo below.下图。

Error reading data读取数据时出错

Any ideas of how could I read the data?关于如何读取数据的任何想法?

Thanks谢谢

Use this example to download the excel from Google Drive (the fileid is the ID after the /d/ part in your URL):使用此示例从 Google Drive 下载 excel( fileid是 URL 中/d/部分后面的 ID):

fileid = "16cp23cJxeyUfnBHMp-sNCuFNQxe8cqOV"

df = pd.read_excel(
    "https://drive.google.com/uc?export=download&id={fileid}".format(
        fileid=fileid
    ),
    skiprows=17,
)
print(df)

Prints:印刷:

     Unnamed: 0                                         Unnamed: 1                                         Unnamed: 2 Petajoules Gigajoules           %
0           NaN                                        Afghanistan                                        Afghanistan        321         10   78.669280
1           NaN                                            Albania                                            Albania        102         35  100.000000
2           NaN                                            Algeria                                            Algeria       1959         51    0.551010
3           NaN                                     American Samoa                                     American Samoa        ...        ...    0.641026
4           NaN                                            Andorra                                            Andorra          9        121   88.695650
5           NaN                                             Angola                                             Angola        642         27   70.909090

...and so on.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM