简体   繁体   中英

How to fix Python TypeError and AttributeError

I am new to Python and I had written some code, when I run the code, I got some compile error. Could someone please help me figure out what went wrong.

This is what contain in my temp.log file:

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)

Some code I have so far: #!/usr/bin/python import re

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

 data = []
 for line in lines:

     date = re.match(r'\d{2} \w+ \d{2}', lines).group(0)
     time = lines.split()[3]
     ids = lines.split()[4]
     agent = re.search(r'agent:\s(.*?),', lines).group(1)        
     errID = re.search(r'ErrIdText:\s(.*?),', lines).group(1)
     clear = re.search(r'clearedID:\s(.*?)\)', lines).group(1)

     row = [date, time, ids, agent, errID, clear]
     data.append(row)

 for row in data:
     print(row)

When I run the above code, I got:

 Traceback (most recent call last):
 File "./practice.py", line 10, in <module>
   date = re.match(r'\d{2} \w+ \d{2}', lines).group(0)
 File "/usr/lib64/pythong2.7/re.py", line 137, in match
   return _compile(patter, flags).match(string)
 TypeError: expected string or buffer

Also, If I comment out line 10, the next error in ran into is:

 Traceback (most recent call last):
 File "./practice.py", line 11, in <module>
 time = lines.split()[3]
 AttributeError: 'list' object has no attribute 'split'

Could anyone please help me figure out these error.

Many thanks in advance

As the errors both say, lines is a list. Presumably you meant to use line throughout inside the for loop.

date = re.match(r'\d{2} \w+ \d{2}', ).group(0)
time = .split()[3]
...

This is the working version of your code. Some typo errors like line instead of lines and group() instead of group(0) but you're doing fine.

import re

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

data = []
for line in lines:
    date = re.match(r'\d{2} \w+ \d{2}', line).group()    
    time = line.split()[3]
    ids = line.split()[4]

    try:
        agent = re.search(r'agent:\s(.*?),', line).group()
    except:
        agent = 'agent:'
    try:
        errID = re.search(r'ErrIdText:\s(.*?),', line).group()
    except:
        errID = 'ErrIdText:'
    try:
        clear = re.search(r'clearedID:\s(.*?)\)', line).group()
    except:
        clear = 'clearedID:'

    row = [date, time, ids, agent, errID, clear]
    data.append(row)

for row in data:
     print(row)

EDIT: Add try and except when keywords are not found in the log file

Result:

['06 May 19', '03:40:35', '3', 'agent: 12367a12,', 'ErrIdText: ERROR ID TEXT,', 'clearedID: 0x089088394)']
['06 May 19', '03:44:35', '3', 'agent: 12368a15,', 'ErrIdText: Skip this item,', 'clearedID: 0x089088394)']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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