简体   繁体   English

python在日志文件中找到失败的数字

[英]python find FAILED numbers in log file

I would like Python code to search for FAILED number from a file.txt .like here 5 , i need to search the FAILED number, whether 0 or any number. 我想要Python代码从file.txt搜索失败的编号。像这里5,我需要搜索失败的编号,无论是0还是任何数字。 then i can send an email the number of failures. 然后我可以向电子邮件发送失败次数。 I have tried the grep but it does not works. 我已经尝试了grep,但是它不起作用。

searchfile = open("file.txt", "r")
for line in searchfile:
    if "FAILED" in line: print line
searchfile.close()


             Total    Copied   Skipped  Mismatch    FAILED    Extras
 Dirs :      2575         0      2575         0         5         0
Files :      6039         0      6039         0         0         0
Bytes :   1.547 g         0   1.547 g         0         0         0
Times :   0:00:53   0:00:00                       0:00:00   0:00:53


    Ended : Tue Aug 30 04:32:48 2016

You can find the position of the FAILED column and look at the values at that position in the rows of interest: 您可以找到FAILED列的位置,并查看感兴趣的行中该位置的值:

result = {}
col = 'FAILED'
col_index = None
row_names = {'Dirs', 'Files'}

for l in open('file.txt').readlines():
    if col_index is None:
        if col in l:
            col_index = l.find(col)
    else:
        l_split = l.split()
        if len(l_split) > 0 and l_split[0] in row_names:
            result[l_split[0]] = int(l[col_index:col_index+len(col)])
print(result)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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