首页 > 解决方案 > Python如何将字符串列表放入html + css字符串?

问题描述

我在 QMessageBox 中有一个使用 css 样式的 html 表,在表中有九个单元格将由变量列表填充,并且我不能将变量放入 html + css 字符串中而不会使程序崩溃。

这个例子:

from PyQt6.QtCore import *
from PyQt6.QtGui import *
from PyQt6.QtWidgets import *

app = QApplication([])

message = QMessageBox()

message.setText(
'<html><body>' \
'Differences between the inputted names and retrieved names have been detected' \
'    <p align="center">Comparison:</p>' \
'    <style>' \
'    table {' \
'               border-width: 2px;' \
'               border-style: groove;' \
'               border-color: #4080c0;' \
'               border-radius: 4px;' \
'               background: #202020;' \
'               color: #00aeef;' \
'           }' \
'    </style>' \
'    <table "cellpadding=3">' \
'        <tr>' \
'            <td width="25%">Field</td>' \
'            <td width="25%">Inputed</td>' \
'            <td width="25%">Equivalence</td>' \
'            <td width="25%">Retrieved</td>' \
'        </tr>' \
'        <tr>' \
'            <td width="25%">Artist</td>' \
'            <td width="25%"><b><i>{0}</b></i></td>' \
'            <td width="25%">{1}</td>' \
'            <td width="25%"><b><i>{2}</b></i></td>' \
'        </tr>' \
'        <tr>' \
'            <td width="25%">Album</td>' \
'            <td width="25%"><b><i>{3}</b></i></td>' \
'            <td width="25%">{4}</td>' \
'            <td width="25%"><b><i>{5}</b></i></td>' \
'        </tr>' \
'        <tr>' \
'            <td width="25%">Title</td>' \
'            <td width="25%"><b><i>{6}</b></i></td>' \
'            <td width="25%">{7}</td>' \
'            <td width="25%"><b><i>{8}</b></i></td>' \
'        </tr>' \
'    </table>' \
'Are you sure the provided information is correct?' \
'</body></html>'.format(*["1", "<b> NOT equals </b>", "Electus", "1", "<b> NOT equals </b>", "Temple of Light", "1", "<b> NOT equals </b>", "Temple of Light"])
)
message.setStandardButtons(QMessageBox.StandardButton.Ok)
message.setTextFormat(Qt.TextFormat.MarkdownText)
message.exec()

我在format方法中放入的列表是程序动态生成的列表,我想要一个优雅的方法来填补空白。

实际运行时,列表是一个变量。

尝试运行代码会导致以下错误:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-1-7082e175a4fa> in <module>
      8
      9 message.setText(
---> 10 '<html><body>' \
     11 'Differences between the inputted names and retrieved names have been detected' \
     12 '    <p align="center">Comparison:</p>' \

KeyError: '               border-width'

现在,如果我将所有{n}s替换为%s并使用字符串插值运算符:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-2-2a3d4e1d6147> in <module>
      8
      9 message.setText(
---> 10 '<html><body>' \
     11 'Differences between the inputted names and retrieved names have been detected' \
     12 '    <p align="center">Comparison:</p>' \

ValueError: unsupported format character '"' (0x22) at index 441

我已经通过删除样式确认数据将正确显示,并且通过删除样式将起作用,但是我怎样才能同时拥有这两者?

标签: pythonpython-3.x

解决方案


在该部分:

table {
    border-width: ...
}

将其替换为

table {{
    border-width: ...
}}

因为python认为它是一个格式字符串,如{}

在此处输入图像描述


推荐阅读