Python Conditionally Add Class to <td> Tags in HTML Table

最后都变了- 提交于 2019-12-25 04:47:09

问题


I have some data in the form of a csv file that I'm reading into Python and converting to an HTML table using Pandas.

Heres some example data:

name  threshold  col1 col2 col3
A     10         12   9    13
B     15         18   17   23
C     20         19   22   25

And some code:

import pandas as pd
df = pd.read_csv("data.csv")
table = df.to_html(index=False)

This creates the following HTML:

<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th>name</th>
      <th>threshold</th>
      <th>col1</th>
      <th>col2</th>
      <th>col3</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>A</td>
      <td>10</td>
      <td>12</td>
      <td>9</td>
      <td>13</td>
    </tr>
    <tr>
      <td>B</td>
      <td>15</td>
      <td>18</td>
      <td>17</td>
      <td>23</td>
    </tr>
    <tr>
      <td>C</td>
      <td>20</td>
      <td>19</td>
      <td>22</td>
      <td>25</td>
    </tr>
  </tbody>
</table>

No I want to conditionally add a class to each cell in the html table if its value is less than a certain threshold. The threshold is different for each row in the table.

So given the example data above, I want to add a class, class="custom" to cell col2 with name A and to cell col1 with name C. In CSS, I will then fill the cell color as red if it has the "custom" class.

The result would be something like:

<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th>name</th>
      <th>threshold</th>
      <th>col1</th>
      <th>col2</th>
      <th>col3</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>A</td>
      <td>10</td>
      <td>12</td>
      <td class="custom">9</td>
      <td>13</td>
    </tr>
    <tr>
      <td>B</td>
      <td>15</td>
      <td>18</td>
      <td>17</td>
      <td>23</td>
    </tr>
    <tr>
      <td>C</td>
      <td>20</td>
      <td class="custom">19</td>
      <td>22</td>
      <td>25</td>
    </tr>
  </tbody>
</table>

How can this be achieved using Beautiful Soup?


回答1:


Using BeautifulSoup you can add a class to the tags attrs as you would set a key/value in a dictionary:

soup = BeautifulSoup(html,"html.parser")

for row in soup.select("tbody tr"):
    tds = row.find_all("td")     
    if int(tds[3].text) < int(tds[1].text):
        tds[3]["class"] = "custom"
     if int(tds[2].text) < int(tds[1].text):
        tds[2]["class"] = "custom"

Which using your input html would give you:

<html><body><table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th>name</th>
<th>threshold</th>
<th>col1</th>
<th>col2</th>
<th>col3</th>
</tr>
</thead>
<tbody>
<tr>
<td>A</td>
<td>10</td>
<td>12</td>
<td class="custom">9</td>
<td>13</td>
</tr>
<tr>
<td>B</td>
<td>15</td>
<td>18</td>
<td>17</td>
<td>23</td>
</tr>
<tr>
<td>C</td>
<td>20</td>
<td class="custom">19</td>
<td>22</td>
<td>25</td>
</tr>
</tbody>
</table></body></html>

Just use whatever it is in the if that decides whether to add it or not.



来源:https://stackoverflow.com/questions/39599802/python-conditionally-add-class-to-td-tags-in-html-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!