【问题标题】:parse .xls file from dropbox link从 Dropbox 链接解析 .xls 文件
【发布时间】:2021-06-30 12:50:39
【问题描述】:

我正在尝试从 Dropbox 链接 (https://www.dropbox.com/s/i77mern7joxc9ur/TestResultCodelistVoC.xlsx) 解析表格。这是一个.xlsx 表,到目前为止我已经尝试了两种方法

方法一

codeID_url = 'https://www.dropbox.com/s/i77mern7joxc9ur/TestResultCodelistVoC.xlsx'

tables = pd.read_html(codeID_url)
df_codeID = tables[0]

给予

ValueError: No tables found

这是有道理的,因为最后,我不是从 html 页面解析表格。上面的命令非常适合本页中的表格 (https://www.ecdc.europa.eu/en/covid-19/variants-concern)

方法 2

codeID_url = 'https://www.dropbox.com/s/i77mern7joxc9ur/TestResultCodelistVoC.xlsx'
data = pd.read_excel(codeID_url,'TestResultCodelistVoC')

给予:

XLRDError: Unsupported format, or corrupt file: Expected BOF record; found b'<!DOCTYP'

我确实找到了关于同一错误 here 的主题,尽管所有答案都在处理 local .xls 文件,而在我的情况下,我正在尝试解析网页/链接,最后是一个.xls 文件。

我也遇到了一个使用 dropbox token 的解决方案,但我首先想尝试在没有 Dropbox 帐户的情况下下载上述表格。

【问题讨论】:

    标签: python excel pandas dropbox


    【解决方案1】:

    ?dl=1 添加到您的网址末尾。

    >>> import pandas as pd
    >>>
    >>> url = 'https://www.dropbox.com/s/i77mern7joxc9ur/TestResultCodelistVoC.xlsx?dl=1'
    >>> df = pd.read_excel(url)
    >>> print(df)
                 Codelistname  Codesystem name  ...                                     Short label DE 1st Release
    0   TestResultCodelistVoC              NaN  ...                                  Confirmed 501Y.V1         NaN
    1   TestResultCodelistVoC              NaN  ...                                  Confirmed 501Y.V2         NaN
    2   TestResultCodelistVoC              NaN  ...                                  Confirmed 501Y.V3         NaN
    3   TestResultCodelistVoC              NaN  ...                               Confirmed 501Y.V3.P1         NaN
    4   TestResultCodelistVoC              NaN  ...                               Confirmed 501Y.V3.P2         NaN
    5   TestResultCodelistVoC              NaN  ...                Confirmed not one of the listed VOC         NaN
    6   TestResultCodelistVoC              NaN  ...                            Compatible with 501Y.V1         NaN
    7   TestResultCodelistVoC              NaN  ...                            Compatible with 501Y.V2         NaN
    8   TestResultCodelistVoC              NaN  ...                            Compatible with 501Y.V3         NaN
    9   TestResultCodelistVoC              NaN  ...                         Compatible with 501Y.V3.P1         NaN
    10  TestResultCodelistVoC              NaN  ...                         Compatible with 501Y.V3.P2         NaN
    11  TestResultCodelistVoC              NaN  ...                          Compatible with 501Y.V2-3         NaN
    12  TestResultCodelistVoC              NaN  ...                              Compatible with a VOC         NaN
    13  TestResultCodelistVoC              NaN  ...                             Confirmed MinkCluster5         NaN
    14  TestResultCodelistVoC              NaN  ...                       Compatible with MinkCluster5         NaN
    15  TestResultCodelistVoC              NaN  ...                        Not compatible with 501Y.V1         NaN
    16  TestResultCodelistVoC              NaN  ...                      Not compatible with 501Y.V2-3         NaN
    17  TestResultCodelistVoC              NaN  ...  No compatibility with VOC detected (VOC not fu...         NaN
    18  TestResultCodelistVoC              NaN  ...                           Other variant of concern         NaN
    
    [19 rows x 12 columns]
    >>>
    

    【讨论】:

    • 你能用这个命令将 url 解析为 df 吗?我收到XLRDError: Excel xlsx file; not supported。我的 xlrd 版本是2.0.1
    • 您需要安装openpyxl。正如错误所说,xlrd 不支持 XLSX 文件。
    • 它甚至在documentation for xlrd 的最顶部这样说 - “xlrd 是一个库,用于从 Excel 文件中读取数据和格式化信息,采用历史 .xls 格式。”
    • 那很奇怪,我确实安装了 openpyxl。 conda list | grep 'openpy 返回openpyxl 3.0.5 py_0 anaconda。我现在使用的命令与您仍然收到XLRDError: Excel xlsx file; not supported 相同。也许这与熊猫版本有关?我有0.25.3
    • 在python脚本中尝试import openpyxl会得到什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-30
    • 2015-03-25
    • 2013-10-09
    • 2021-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多