python - 如何在python中用HTML标签准确地包围单词

标签 python string

现在我有这些变量和它们各自的值。

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>为了方便。
现在最初我只想有这样的输出。 (见图)
enter image description here
但是之前 我们可以使用 来实现我们需要将这些词括起来的图像上的内容。正确 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/

相关文章:

python - 为 Django 中两个不同类的选项创建外键

string - 通过正则表达式拆分字符串以获得 Vec<String>

Javascript 多行字符串和意外 token ILLEGAL

python - 从列表创建的表未显示所有元素

使用 selenium : ui-dialog trouble with switching 进行 Python 3 网页抓取

python - 从python中的文件名中检索单词

python - BeautifulSoup 迭代不起作用

java - 有没有更好的方法从多个字符串数组中查找任何字符串

python - 将字符串格式化为 Python 中的特定字符限制

string - 当我从 Excel 复制并粘贴到记事本时,每个引号后都会有额外的引号