繁体   English   中英

在特定字词之后查找所有字词,并将其替换为HTML代码

[英]Find all words after a specific word and replace it with HTML code

我有一个日志文件:

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)

我的代码当前为所有行添加了悬停功能,但是我只希望它在USER INPUT之后的所有内容中使用:“ .....”任何建议都将非常有用! 谢谢! 请随时问我任何问题或澄清

这是您想要的吗?

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM