gpt4 book ai didi

python - 查找特定单词之后的所有单词并将其替换为 HTML 代码

转载 作者:行者123 更新时间:2023-12-01 01:43:41 25 4
gpt4 key购买 nike

我有一个日志文件:

USER INPUT :  “clear”                   
SYSTEM RESPONSE: “Hello! How are you?”
USER INPUT : “Good thank you”
SYSTEM RESPONSE: "Okay"
USER INPUT : “clear”
SYSTEM RESPONSE: “Hello! How are you?”
USER INPUT : “I am good, Thank you!”
SYSTEM RESPONSE: "Great!"
USER INPUT : “Good”

我当前的代码是 python 和 html :

import re
from pprint import pprint


log_file = """USER INPUT : "clear"
SYSTEM RESPONSE: Hello! How are you?
USER INPUT : Good thank you
SYSTEM RESPONSE: Okay
USER INPUT : "clear"
SYSTEM RESPONSE: Hello! How are you?
USER INPUT : I am good, Thank you!
SYSTEM RESPONSE: Great!
USER INPUT : Good"""


groups = re.findall(r'USER INPUT.*?clear.*?(?:(?=USER INPUT :\s+\Wclear\W)|(?=\Z))', log_file, flags=re.DOTALL)

html=''
data = []
for d, g in zip(data, groups):
for line in groups.splitlines():
html += """<p class = " tooltip" style="color:green;" >"""+ line + '<span class=" tooltiptext">Tooltip text</span> </p>\n'

pprint("""<html>
<style>
.tooltip {
position: relative;
display: inline-block;
border-bottom: 1px dotted black;

}

.tooltip .tooltiptext {
visibility: hidden;
width: 120px;
background-color: black;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 5px 0;

/* Position the tooltip */
position: absolute;
z-index: 1;
}

.tooltip:hover .tooltiptext {
visibility: visible;
}
</style><body>"""+ html)

我的代码当前为所有行添加了悬停功能,但我只希望它用于用户输入之后的任何内容:“.....”任何建议都会很棒!谢谢你!如果有任何问题或需要说明,请随时向我询问

最佳答案

这符合你的要求吗?

for d, g in zip(data, groups):
for line in groups.splitlines():
if line.startswith("USER INPUT"):
html += """<p class = " tooltip" style="color:green;" >"""+ line + '<span class=" tooltiptext">Tooltip text</span> </p>\n'
else:
html += """<p style="color:green;" >"""+ line + </p>\n'

关于python - 查找特定单词之后的所有单词并将其替换为 HTML 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51620155/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com