[英]Find all words after a specific word and replace it with HTML code
I have a 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”
and my current code for them is in python and html : 我目前的代码是在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)
My code currently adds the hover feature for all the lines, but I only want it for anything that follows USER INPUT: "....." Any suggestions will be great! 我的代码当前为所有行添加了悬停功能,但是我只希望它在USER INPUT之后的所有内容中使用:“ .....”任何建议都将非常有用! Thank you!
谢谢! Please feel free to ask me for any questions or clarifications
请随时问我任何问题或澄清
Does this do what you want? 这是您想要的吗?
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'
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 g.splitlines():
if "USER INPUT" in line :
html += stylep[d] + "USER INPUT" + line[10:] + '<span class=" tooltiptext">Tooltip text</span> </p>\n'
else:
html += style[d]+ line + '</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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.