簡體   English   中英

Python:如何從 web 導入 excel 文件?

[英]Python: how import excel file from the web?

我需要從鏈接中導入 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' XLRDError: Unsupported format, or corrupt file: Expected BOF record; found b'Exchange'

然后我找到了使用 Pandas 從 URL 讀取 excel 文件的以下方法 - 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

這里還有一個奇怪的output。 從 web 手動下載時,如何獲得下表?

您的文件不是 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 會迭代可能的格式,直到找到可行的格式。 例如,您可以制作一個制表符分隔值(應該有 extension.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)

然后使用 Pandas 讀取它:

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

這會給你:

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

用真正的 Excel 檢查后,我發現:

在此處輸入圖像描述

所以我懷疑你的文件有問題。 使用正確的文件,上述方法應該可以工作。

安裝requests時,最新的 pandas (pandas==1.2.0) 能夠使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM