Python:如何从web导入excel文件?

2024-09-29 17:24:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要从链接导入excel文件。我试着用它来做

filedlurl = 'https://www.nordpoolgroup.com/48d3ac/globalassets/marketdata-excel-files/exchange-ee-connections_2021_daily.xls'
    
df = pd.read_excel(filedlurl, skiprows=2)

但是错误是XLRDError: Unsupported format, or corrupt file: Expected BOF record; found b'Exchange'

然后我找到了下面的方法 using Pandas to read in excel file from URL - XLRDError

df = pd.read_csv('https://www.nordpoolgroup.com/48d3ac/globalassets/marketdata-excel-files/exchange-ee-connections_2021_daily.xls',
                 #sep='\t',
                 #parse_dates=[0],
                 names=['a','b','c','d','e','f'],
                skiprows=2)
df

这里还有一个奇怪的输出。 从web手动下载时,如何获取下表


Tags: httpscomdfreadexchangewwwfilesexcel
3条回答

最新的pandas(pandas==1.2.0)能够在安装requests时使用pd.read_excel从url读取

import pandas
url = "https://www.nordpoolgroup.com/48d3ac/globalassets/marketdata-excel-files/exchange-ee-connections_2021_daily.xls"
df = pandas.read_excel(url)

但您的文件似乎已损坏,它会触发:

ValueError: File is not a recognized excel file

您的文件不是CSV或Excel文件。实际内容是一个HTML表(如下所示)

Exchange in {0}, Import(+)/Export(-)
<html>
    <body>
        <table>
            <thead>
                <tr>
                    <td colspan="5">Exchange EE connections in MWh, MW</td>
                </tr><tr>
                    <td colspan="5">Data was last updated 06-01-2021</td>
                </tr><tr>
                    <td></td><td style="text-align:center;">EE net exchange</td><td style="text-align:center;">EE - FI</td><td style="text-align:center;">EE - LV</td><td style="text-align:center;">EE - RU</td>
                </tr>
            </thead><tbody>
                <tr>
                    <td style="text-align:left;">01-01-2021</td><td style="text-align:right;">14575</td><td style="text-align:right;">20969,0</td><td style="text-align:right;">-4884,0</td><td style="text-align:right;">-1510,0</td>
                </tr><tr>
                    <td style="text-align:left;">02-01-2021</td><td style="text-align:right;">12073</td><td style="text-align:right;">22479,0</td><td style="text-align:right;">-8001,0</td><td style="text-align:right;">-2405,0</td>
                </tr><tr>
                    <td style="text-align:left;">03-01-2021</td><td style="text-align:right;">14321</td><td style="text-align:right;">22540,0</td><td style="text-align:right;">-8259,0</td><td style="text-align:right;">40,0</td>
                </tr><tr>
                    <td style="text-align:left;">04-01-2021</td><td style="text-align:right;">14662</td><td style="text-align:right;">17653,0</td><td style="text-align:right;">-5829,0</td><td style="text-align:right;">2838,0</td>
                </tr><tr>
                    <td style="text-align:left;">05-01-2021</td><td style="text-align:right;">13570</td><td style="text-align:right;">13779,0</td><td style="text-align:right;">-5314,0</td><td style="text-align:right;">5105,0</td>
                </tr><tr>
                    <td style="text-align:left;">06-01-2021</td><td style="text-align:right;">6243</td><td style="text-align:right;"></td><td style="text-align:right;"></td><td style="text-align:right;"></td>
                </tr>
            </tbody>
        </table>
    </body>
</html>

像这样使用pd.read_html

import pandas as pd

url = 'https://www.nordpoolgroup.com/48d3ac/globalassets/marketdata-excel-files/exchange-ee-connections_2021_daily.xls'
dfs = pd.read_html(url)
df = dfs[0]

您可以在Excel中打开文件是因为Excel会迭代可能的格式,直到找到有效的格式。例如,你可以制作一个标签分隔的值(应该有扩展名.tsv)文件,append.xls,虽然它不是一个真正可怕的电子表格格式(xls),Excel仍然会正常打开它。它还可以对HTML数据执行此操作

首先,可以通过以下方式使用Python下载文件:urllib.request

import urllib.request
url='https://www.nordpoolgroup.com/48d3ac/globalassets/marketdata-excel-files/exchange-ee-connections_2021_daily.xls'
filename='yourfile.csv'
urllib.request.urlretrieve (url,filename)

然后在使用熊猫中阅读:

import pandas as pd
df = pd.read_excel(filename)

这将给你:

XLRDError: Unsupported format, or corrupt file: Expected BOF record; found b'Exchange'

在使用真正的Excel进行检查后,我发现:

enter image description here

我怀疑你的档案有问题。对于正确的文件,上述方法应该有效

相关问题 更多 >

    热门问题