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

时间:2018-07-31 18:47:54

标签: python html python-2.7

我有一个日志文件:

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之后的所有内容中使用它:“ .....” 任何建议都会很棒!谢谢!如有任何疑问或澄清,请随时问我

2 个答案:

答案 0 :(得分:1)

这是您想要的吗?

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'

答案 1 :(得分:1)

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)