Python有条件地将类添加到< td> HTML表中的标签 [英] Python Conditionally Add Class to <td> Tags in HTML Table

查看:263
本文介绍了Python有条件地将类添加到< td> HTML表中的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些csv文件形式的数据,我正在将这些数据读入Python并使用Pandas转换为HTML表.

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.

这里有一些示例数据:

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

和一些代码:

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

这将创建以下HTML:

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>

否,如果它的值小于某个阈值,我想有条件地向html表中的每个单元格添加一个类.表中的每一行的阈值都不同.

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.

因此,鉴于上述示例数据,我想向名称为A的单元格col2和名称为C的单元格col1添加一个类class ="custom".在CSS中,如果该单元格颜色为红色,则将其填充为红色具有自定义"类.

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.

结果将类似于:

<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?

推荐答案

使用 BeautifulSoup ,您可以在标签attrs中添加一个类,就像在字典中设置键/值一样:

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"

使用您输入的html可以给您:

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.

这篇关于Python有条件地将类添加到&lt; td&gt; HTML表中的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆