从日志文件中删除不需要的字符

时间:2019-05-21 13:30:55

标签: python regex shell

我正在编写一个Python脚本来读取文件,逐行读取该文件,并通过用户命令行参数将数据从该文件解析到另一个文本文件。我快结束了,但是我的日志文件中有一些奇怪的字符“ ^ @”,我不想包含在输出文件中。

temp.log:

06 May 19 03:40:35^@ 3 abCodeClearTrap Error Clear Trap (agent: 12367a12,
chassis:12367a12, ErrIdText: ERROR ID TEXT, csssi: EXTIFG, clearedID:
0x089088394)

06 May 19 03:44:35^@ 3 abCodeErrorTrap Error Trap (agent: 12368a15,
chassis:12368a15, ErrIdText: Skip this item, csssi: SSRSSR, clearedID:
0x089088394)

我的代码:

import re, sys

with open('temp.log') as f:
   lines = f.readlines()

with open('output.txt') as o:

    data = []
    for line in lines:
       if 'date' in sys.argv:
           try:
              date = re.match(r'\date{2} \w+ \date{2}', line).group()
              row.append(date)
           except:
              date = 'date'
       if 'time' in sys.argv:
           try:
               time = line.split()[3]
               row.append(time)
               lst = [x.replace("^@","") for x in lst]
               row.append(",")
           except:
               time = 'time'
       if 'agent' in sys.argv:
           try:
              agent = re.search(r'agent:\s(.*?),', line).group()
              row.append(agent)
           except:
              agent = 'agent:'
       if 'err' in sys.argv:
           try:
              errID = re.search(r'ErrIdText:\s(.*?),', line).group()
              row.append(errID)
           except:
              errID = 'ErrIdText:'
       if 'clear' in sys.argv:
           try:
              clear = re.search(r'clearedID:\s(.*?)\)', line).group()
              row.append(clear)
           except:
              clear = 'clearedID:'

       row = []
       data.append(row)

     for row in data:
        lines = o.writelines(row)
        lines = o.writelines("\n")
        print(row)

     o.close()

如果要处理时间,我尝试替换掉它,但似乎不起作用。

0 个答案:

没有答案