簡體   English   中英

從 python 中的 URL 讀取 xls 文件

[英]Read xls file from a URL in python

我正在嘗試從 python https://drive.google.com/file/d/16cp23cJxeyUfnBHMp-sNCuFNQxe8cqOV/view中的鏈接讀取數據

我試過這個:

import pandas as pd

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

這返回了這個錯誤:

XLRDError:不支持的格式,或損壞的文件:預期的 BOF 記錄; 找到 b'<!DOCTYP'

然后我嘗試使用 cvs 格式

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

退了這個

ParserError:錯誤標記數據。 C 錯誤:預計第 133 行中有 298 個字段,看到 440

最后我試過這個:

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

此讀取數據,但這不是我在看到鏈接后所期望的(283 行,7 列)。 下圖。

讀取數據時出錯

關於如何讀取數據的任何想法?

謝謝

使用此示例從 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)

印刷:

     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