Text File Parsing with Python?! unique pattern of txt words

Viewed 96

I am trying to parse a series of messages from the text file and save them as txt files using Python (2.7.3) or any other python versions.

I have txt file like this .txt :

[#11:23][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
INFO isn't NULL
[#12:25][PERFECT][0x0015a] process returned as NULL load index[1] , length[20] , type[0]
[#13:3][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
PERFECT isn't NULL
[#4:23][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
Time is here [Tick:135055] , Time:  17, index: 608, CastedType:20002, area :0
[#15:23][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
[#16:25][PERFECT][0x0015a] process returned as NULL load index[1] , length[20] , type[0]
[#17:3][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
[#8:23][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
time is here [Tick:135055] , Time:  17, index: 608, CastedType:20002, area :0
[#16:23][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
[#14:25][PERFECT][0x0015a] process returned as NULL load index[1] , length[20] , type[0]
[#18:3][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
[#6:23][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
Time is here [Tick:135055] , Time:  17, index: 608, CastedType:20002, area :0

this is the type formats of all rows that txt have , so each row is repeated on given txt file and it has its own unique pattern as I showed above, where the key words [INFO] , [PERFECT] are not changed per the message those key words values are not changed in this message pattern. consider each row is a new message , so at each row there is a new message starts.

what Im trying to implement in python a function that reads line by line the txt file and all rows there has this types of patterns as I mentioned above and to dump all rows in this certain type:

[#12:25][PERFECT][0x0015a] process returned as NULL load index[1] , length[20] , type[0]

to another txt file. so if I go to another txt file I shall see all rows there has this type of messages:

[#12:25][PERFECT][0x0015a] process returned as NULL load index[1] , length[20] , type[0]

Now after sniffing this type of message from the given txt(input txt) , I need to read line by line the new txt file that I generated that has the certain message type and then take the load index values and dump them in another txt file that has just the values of load index.

So in my example above I shall get like this:

Given txt file :(this is .txt file as input)

[#11:23][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
INFO isn't NULL
[#12:25][PERFECT][0x0015a] process returned as NULL load index[1] , length[20] , type[0]
[#13:3][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
PERFECT isn't NULL
[#4:23][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
Time is here [Tick:135055] , Time:  17, index: 608, CastedType:20002, area :0
[#15:23][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
[#16:25][PERFECT][0x0015a] process returned as NULL load index[1] , length[20] , type[0]
[#17:3][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
[#8:23][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
time is here [Tick:135055] , Time:  17, index: 608, CastedType:20002, area :0
[#16:23][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
[#14:25][PERFECT][0x0015a] process returned as NULL load index[1] , length[20] , type[0]
[#18:3][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
[#6:23][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
Time is here [Tick:135055] , Time:  17, index: 608, CastedType:20002, area :0

Results/output of the function:

  1. generating txt file that has all rows of the certain pattern that I explained above (all rows that has word [PERFECT] so the generated txt file shall be having all messages / rows that has [PERFECT] :

    [#12:25][PERFECT][0x0015a] process returned as NULL load index[1] , length[20] , type[0] [#16:25][PERFECT][0x0015a] process returned as NULL load index[1] , length[20] , type[0] [#14:25][PERFECT][0x0015a] process returned as NULL load index[1] , length[20] , type[0]

  2. Then generating a another new txt file for the load index values which in my case load index values found inside [ ] of the word load index ( load index [value] ), so the function shall dump in new txt file the values of the load index as column into the another new generated txt file :

1 1 1

How to parse in python a text file containing this patterns and message rows as I explained above?

In simple words , I want to run row by row(message by message) over the given txt file with the message patterns as I explained above, then parsing into new txt file all the messages that has the keyword [PERFECT] with Brackets , so I will have in new generated txt file only messages that has keyword [PERFECT] . Now after having this new generated file that has only sniffed the messages that has keyword [PERFECT] then to loop and pass over each message in this new generated file (that has the sniffed messages with unique pattern [PERFECT] ) to get the values of the load index [value] that's appear in each message as in my case it's 1 1 1 since load index [1] appear as 1 in three messages . the load index values shall be dumped in another new txt file that has as column the values of load index.

thanks alot for any cooperation !

1 Answers
def get_statuses(s, t):
    statuses = []
    for line in s.splitlines():
        if line.startswith("[#"):
            meta, content = line.split(" ", 1)
            time, status, code = meta.split("][")
            time, code = time[2:], code[:-1]
            index = re.search(r'(index\[)(\d+)(\])', content).group(2)
            if status == t:
                statuses.append({
                    'time': time, 'code': code, 'content': content, 'index': index
                })
    return statuses

It will output:

[{'time': '12:25',
  'code': '0x0015a',
  'content': 'process returned as NULL load index[1] , length[20] , type[0]',
  'index': '1'},
 {'time': '16:25',
  'code': '0x0015a',
  'content': 'process returned as NULL load index[1] , length[20] , type[0]',
  'index': '1'},
 {'time': '14:25',
  'code': '0x0015a',
  'content': 'process returned as NULL load index[1] , length[20] , type[0]',
  'index': '1'}]

You can use function output for csv.DictWriter().

Related