[英]Python pattern match from a file
Experts, I am Just trying to match the pattern from my raw data
file so as to list the not running service into html
format.专家们,我只是想匹配
raw data
文件中的模式,以便将未运行的服务列出为html
格式。
I have took the help from the googling and using something like below but its not working, any help on this will be greatful.我已经从谷歌搜索中获得了帮助,并使用了类似下面的东西,但它不起作用,对此的任何帮助都会非常有用。
code:代码:
Html_file= open("result1.html","w")
html_str = """
<table border=1>
<tr>
<th bgcolor=fe9a2e>Hostname</th>
<th bgcolor=fe9a2e>Service</th>
</tr>
"""
Html_file.write(html_str)
fh=open(sys.argv[1],"r")
for line in fh:
pat_match=re.match("^HostName:"\s+(.*?)\".*", line)
pat_match1=re.match("^Service Status:"\s+(.*not.*?)\".*", line)
if pat_match:
Html_file.write("""<TR><TD bgcolor=fe9a2e>""" + pat_match.group(1) + """</TD>\n""")
elif pat_match1:
Html_file.write("""<TR><TD><TD>""" + pat_match1.group(2) + """</TD></TD></TR>\n""")
raw data:原始数据:
HostName: dbfoxn001.examle.com
Service Status: NTP is Running on the host dbfoxn001.examle.com
Service Status: NSCD is not Running on the host dbfoxn001.examle.com
Service Status: SSSD is Running on the host dbfoxn001.examle.com
Service Status: Postfix is Running on the host dbfoxn001.examle.com
Service Status: Automount is Running on the host dbfoxn001.examle.com
HostName: dbfoxn002.examle.com SSH Authentication failed
Required Result:要求的结果:
Hostname Service
dbfoxn001.examle.com NSCD is not Running on the host dbfoxn001.examle.com
Your first problem is that your regex is not properly embedded in a string.您的第一个问题是您的正则表达式未正确嵌入字符串中。 You need to either escape or remove the offending
"
s.您需要逃避或删除有问题的
"
s。
Other than that, the actual regex doesn't really match your input data (for example, you are trying to match some "
s which aren't in your input data. I have written regexes as such:除此之外,实际的正则表达式与您的输入数据并不真正匹配(例如,您试图匹配一些不在输入数据中的
"
s。我已经编写了正则表达式:
^HostName:\s*(.+)
^Service Status:\s*(.+is not Running.*)
You can try them here and here .你可以在这里和这里试试。
Lastly, your python code for generating the html seems to not generate the sort of html you want.最后,用于生成 html 的 python 代码似乎无法生成您想要的 html 。 My assumption on how the html of your sample table should look like is as follows:
我对示例表的 html 的假设如下所示:
<table border=1>
<tr>
<th bgcolor=fe9a2e>Hostname</th>
<th bgcolor=fe9a2e>Service</th>
</tr>
<tr>
<td>dbfoxn001.examle.com</td>
<td>NSCD is not Running on the host dbfoxn001.examle.com</td>
</tr>
</table>
To that end I have put the hostname
into its own variable rather than writing it to the file and added it each time a status is parsed.为此,我将
hostname
放入其自己的变量中,而不是将其写入文件并在每次解析状态时添加它。 I have also added the missing final </table>
and closed the open files:我还添加了缺少的最终
</table>
并关闭了打开的文件:
import sys
import re
result = open("result1.html","w")
table_header = """
<table border=1>
<tr>
<th bgcolor=fe9a2e>Hostname</th>
<th bgcolor=fe9a2e>Service</th>
</tr>
"""
result.write(table_header)
input_file=open(sys.argv[1],"r")
for line in input_file:
host_match = re.match("^HostName:\s*(.+)", line)
status_match = re.match("^Service Status:\s*(.+is not Running.*)", line)
if host_match:
hostname = host_match.group(1)
elif status_match:
result.write("""<tr><td>""" + hostname + """</td><td>""" + status_match.group(1) + """</td></tr>\n""")
result.write("""</table>"""
input_file.close()
result.close()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.