Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Parsing a syslog file
#11
ok, I don't fully understand why it won't work on Linux but adding the filter if '[audit]' in data[index]: fixed it Undecided Thank you all!

 
for index, line in enumerate(data):
     try:
         if '[audit]' in data[index]:
           res = data[index].strip().split('[audit] ')[1]
           result = literal_eval(res)
           timestamp = result['timestamp']
           user = result['user']['username']
           print(timestamp, user)
     except KeyError:
         pass
Reply
#12
It don't work because of first line in last log you posted.
Learn to take part out and test them,audi has no [] and not in same place here.
>>> data = "Oct 10 11:42:42 washup20 kernel: [    0.044121] audit: initializing netlink subsys (disabled)\n"
>>> res = data[0].strip().split('[audit] ')[1]
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
IndexError: list index out of range
So what you have done work,or could just add:
except (KeyError, IndexError): 
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Video doing data treatment on a file import-parsing a variable EmBeck87 15 2,914 Apr-17-2023, 06:54 PM
Last Post: EmBeck87
  Modify values in XML file by data from text file (without parsing) Paqqno 2 1,703 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  Parsing xml file deletes whitespaces. How to avoid it? Paqqno 0 1,045 Apr-01-2022, 10:20 PM
Last Post: Paqqno
Thumbs Up Parsing a YAML file without changing the string content..?, Flask - solved. SpongeB0B 2 2,286 Aug-05-2021, 08:02 AM
Last Post: SpongeB0B
  Syslog server Fifoux082 5 2,800 Sep-15-2020, 07:08 PM
Last Post: Fifoux082
  File Name Parsing millpond 5 3,635 Aug-26-2020, 08:04 AM
Last Post: bowlofred
  Error while parsing tables from docx file aditi 1 3,743 Jul-14-2020, 09:24 PM
Last Post: aditi
  help parsing file aslezak 2 2,250 Oct-22-2019, 03:51 PM
Last Post: aslezak
  Python Script for parsing dictionary values from yaml file pawan6782 3 4,965 Sep-04-2019, 07:21 PM
Last Post: pawan6782
  Parsing an MBOX file Oliver 1 8,204 May-26-2019, 07:12 AM
Last Post: heiner55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020