现在我有这些变量和它们各自的值。
s = '''
vinyl I had to go to Miami. The size of the ball is huge also the vinyl cutters.
I have a computer and it is only 1.
Another vinyl
vinylDiesel
'''
data =[
"vinyl",
"size",
"vinyl cutters",
"computer",
"1",
"vinyl",
"5"
]
现在我想要发生的是 每一个字在 数据可以用特定的 包围的变量HTML 标签 在 "s"多变的。现在请注意,标签在很大程度上取决于我想要什么,但对于这个例子,让我们只使用 <tag></tag>
& <sub></sub>
为了方便。现在最初我只想有这样的输出。 (见图)
但是之前 我们可以使用 来实现我们需要将这些词括起来的图像上的内容。正确 HTML 标签 . 这是为什么? , 这是因为我试图在 PYQT5 QTextEdit Widget 中显示结果.因为使用 HTML 是添加一些样式表的方法,所以这就是我正在做的。
现在为了在图像中获得该结果。我需要帮助来创建一个可以生成这样的输出的程序。
预期输出:
(<tag>vinyl<tag>)<sub>1</sub> I had to go to Miami. The (<tag>size</tag>)<sub>2</sub> of the ball is huge also the (<tag>(<tag>vinyl</tag>)<sub>1</sub> cutters</tag>)<sub>3</sub>.
I have a (<tag>computer</tag>)<sub>4</sub> and it is only (<tag>1</tag>)<sub>5</sub>.
Another (<tag>vinyl<tag>)<sub>1</sub>
(<tag>vinyl</tag>)<sub>1</sub>Diesel
然后一旦完成,我就可以简单地设置 QTextEdit 的 HTML 代码。小部件到 的那个预期输出 然后我们将获得图像的输出。到目前为止我尝试过的。
import re
s = '''
vinyl I had to go to Miami. The size of the ball is huge also the vinyl cutters.
I have a computer and it is only 1.
Another vinyl
vinylDiesel
'''
data =[
"vinyl",
"size",
"vinyl cutters",
"computer",
"1",
"vinyl",
"5"
]
for i,p in enumerate(data):
name = p
html_element_name ="span"
color = "blue"
html_attrs={"style": f"color:{color};font-weight: bold;"}
sub_num = f"<sub style='font-weight:bold;font-size:15px;'>{i+1}</sub>"
html_start_tag = '('+"<" + html_element_name + " " + " ".join(["%s='%s'" % (k, html_attrs[k]) for k in html_attrs]) + ">"
html_end_tag = "</" + html_element_name + ">"+')'+sub_num
to_replace_with = ' '+html_start_tag+f"{name}"+html_end_tag+' '
s = re.sub(fr"{name}",to_replace_with, s)
print(s)
最佳答案
您可以使用递归:
def to_tags(s, data, p = []):
new_s = ''
while s:
if (k:=[(i, a) for i, a in enumerate(data, 1) if s.startswith(a) and a not in p]):
i, sb = max(k, key=lambda x:len(x[-1]))
new_s += f'(<tag>{to_tags(sb, data, p + [sb])}</tag>)<sub>{i}</sub>'
s = s[len(sb):]
else:
new_s, s = new_s+s[0], s[1:]
return new_s
print(to_tags(s, data))
输出:'\n(<tag>vinyl</tag>)<sub>1</sub> I had to go to Miami. The (<tag>size</tag>)<sub>2</sub> of the ball is huge also the (<tag>(<tag>vinyl</tag>)<sub>1</sub> cutters</tag>)<sub>3</sub>.\nI have a (<tag>computer</tag>)<sub>4</sub> and it is only (<tag>1</tag>)<sub>5</sub>.\nAnother (<tag>vinyl</tag>)<sub>1</sub>\n\n(<tag>vinyl</tag>)<sub>1</sub>Diesel\n'
关于python - 如何在python中用HTML标签准确地包围单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69377077/