亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 pandas 從文件中提取 html 表?

如何使用 pandas 從文件中提取 html 表?

守候你守候我 2023-09-19 17:33:56
我是 pandas 新手,我正在嘗試從一些 HTML 文件中提取一些數據。如何轉換多個 HTML 表,如下所示:       PS4Game Name | PriceGoW       | 49.99FF VII R  | 59.99       XBXGame Name | PriceGears 5   | 49.99Forza 5   | 59.99<table>  <tr colspan="2">    <td>PS4</td>  </tr>  <tr>    <td>Game Name</td>    <td>Price</td>  </tr>  <tr>    <td>GoW</td>    <td>49.99</td>  </tr>  <tr>    <td>FF VII R</td>    <td>59.99</td>  </tr></table><table>  <tr colspan="2">    <td>XBX</td>  </tr>  <tr>    <td>Game Name</td>    <td>Price</td>  </tr>  <tr>    <td>Gears 5</td>    <td>49.99</td>  </tr>  <tr>    <td>Forza 5</td>    <td>59.99</td>  </tr></table>像這樣的 json 對象:[  { "Game Name": "Gow", "Price": "49.99", "platform": "PS4"},  { "Game Name": "FF VII R", "Price": "59.99", "platform": "PS4"},  { "Game Name": "Gears 5", "Price": "49.99", "platform": "XBX"},  { "Game Name": "Forza 5", "Price": "59.99", "platform": "XBX"}]我嘗試使用 pandas.read_html(path/to/file) 加載包含表的 html 文件,它確實返回了 DataFrame 列表,但我不知道之后如何提取數據,特別是平臺名稱位于標題而不是作為單獨的列。我使用 pandas 是因為我從包含其他形式的表格和 HTML 代碼的本地 htm 文件中提取這些表格,所以我使用:tables = pandas.read_html(file_path, match="Game Name")使用基于該列名稱的匹配參數快速隔離我需要的表。
查看完整描述

1 回答

?
紅顏莎娜

TA貢獻1842條經驗 獲得超13個贊

import pandas as pd


# list to save all dataframe from all tables in all files

df_list = list()


# list of files to load

list_of_files = ['test.html']


# iterate through your files

for file in list_of_files:

    

    # create a list of dataframes from the tables in the file

    dfl = pd.read_html(file, match='Game Name')

    

    # fix the headers and columns

    for d in dfl:


        # select row 1 as the headers

        d.columns = d.iloc[1]


        # select row 0, column 0 as the platform

        d['platform'] = d.iloc[0, 0]


        # selection row 2 and below as the data, row 0 and 1 were the headers

        d = d.iloc[2:]


        # append the cleaned dataframe to df_list

        df_list.append(d.copy())

        

# create a single dataframe

df = pd.concat(df_list).reset_index(drop=True)


# create a list of dicts from df

records = df.to_dict('records')


print(records)

[out]:

[{'Game Name': 'GoW', 'Price': '49.99', 'platform': 'PS4'},

 {'Game Name': 'FF VII R', 'Price': '59.99', 'platform': 'PS4'},

 {'Game Name': 'Gears 5', 'Price': '49.99', 'platform': 'XBX'},

 {'Game Name': 'Forza 5', 'Price': '59.99', 'platform': 'XBX'}]


查看完整回答
反對 回復 2023-09-19
  • 1 回答
  • 0 關注
  • 116 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號