如何从SAP下载的htm文件创建数据帧

2024-04-19 20:46:41 发布

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

我需要从这个HTML文件创建一个数据帧。 enter image description here

我用beautifulSoup尝试了一些代码,但不起作用。另外,我不知道如何创建数据框,因为我只知道只读html,但文件是html

   table = BeautifulSoup(open('D:\DownloadingEmail\Job WSCZ_PO_STATUS_REPORT, Step 1.htm','r', encoding="utf8").read()).find_all()
df = pd.read_html(table)

我得到了TypeError:无法读取'ResultSet'类型的对象


Tags: 文件数据代码imagereadherehtmltable
1条回答
网友
1楼 · 发布于 2024-04-19 20:46:41

这两行代码有很多问题。首先,您尝试将htm存储为一个soup对象,然后再不做任何事情来解析表。你知道吗

其次,你试着把htm读入pandas,但此时它不是htm,而是beautifulSoup对象。你知道吗

您应该能够以字符串的形式读入html,然后使用pandas.read_html()来解析表(注意:pandas在引擎盖下使用BeautifulSoup)。你知道吗

这将返回数据帧列表(所有<table>标记)。然后,我将所有这些数据添加到最后一个数据帧中:

import pandas as pd

htmStr = open('D:\DownloadingEmail\Job WSCZ_PO_STATUS_REPORT, Step 1.htm','r', encoding="utf8").read()
list_of_dataframes = pd.read_html(htmStr)

df = pd.concat(list_of_dataframes)
df.columns = df.iloc[0]
df = df[1:]

输出:943行中的前5行

print (df.head(5).to_string())
0 Doc. Type Pur. Doc. Item        Material Stor. location Order Quantity GR Quantity Open Quantity Outbound DN Quantity PGI Quantity Incoterms1              Pur. Group Name  Vendor Document Date    ETA Date                     Remark                     Vendor Name Currency Unit Price Pur. Group(PO) Pur. Group(Material)      Pur. Group Name Your reference Our reference Shipping instructions: Descr.
1       ZUC  14439416   10      L15813-002           K131             10           0            10                   10           10        FCA  Juliet z zhu 祝慧君#1824 #1824    F040    2019/09/26  2019/11/17                        NaN  SMS Infocomm Global Service(CQ      USD   16.77000            76K                  76C         Lukas Zezula       Critical      Critical                        By AIR
2       ZUC  14439416   20      756743-001           K131             15           0            15                   15            0        FCA  Juliet z zhu 祝慧君#1824 #1824    F040    2019/09/26  2020/01/01                        NaN  SMS Infocomm Global Service(CQ      USD   16.00000            76K                  753  Sonia Chou 周曉婷#5510       Critical      Critical                        By AIR
3       ZUC  14439430   10    6026B0256601           K13R             20           0            20                    0            0        DAP                 Petr Studeny    F0B0    2019/09/26  2019/10/25        C1R7 SP BUFFER K137  Wistron InfoComm (Philippines)      USD    0.24179            76J                  NaN                  NaN           C1R7          C1R7                        By AIR
4       ZUC  14439435   10  074.02260.0A43           K13R             30           0            30                   30            0        DAP                 Petr Studeny    F603    2019/09/26  2019/10/26  C1R7 SHORTAGE URGENT K137             Wistron Corporation      USD    0.14834            76J                  NaN                  NaN           C1R7          C1R7                        By AIR
5       ZNB  14439467   10      826376-001           K131              2           0             2                    0            0        EXW  Juliet z zhu 祝慧君#1824 #1824  960125    2019/09/26  2019/09/30                        NaN             SMS(KUNSHAN)Co.,LTD      USD  110.30000            76K                  753  Sonia Chou 周曉婷#5510             CN      Critical                        By AIR

相关问题 更多 >