Skip to content

Instantly share code, notes, and snippets.

@smeeklai
Created February 27, 2019 03:54
Show Gist options
  • Save smeeklai/d92f2cd19be1767784cbf6b96031f647 to your computer and use it in GitHub Desktop.
Save smeeklai/d92f2cd19be1767784cbf6b96031f647 to your computer and use it in GitHub Desktop.
with open('log.txt', 'r') as file:
for line in file.readlines():
print(line)
start_point = 0
words = line.split(' ')
results = []
for i in range(0, len(words)):
word = words[i]
if ':' in words[i]:
temp = dict()
temp['time'] = words[i]
temp['duration'] = int(words[i+1])
temp['msg'] = ' '.join(words[start_point:i])
start_point = i + 2
results.append(temp)
if 'error' in temp['msg']:
print('msg contains error:', temp)
print('\n')
sorted_results = sorted(results, key=lambda x: x['duration'], reverse=True)
print(sorted_results)
@smeeklai
Copy link
Author

i'm started 18:00 123 processing 18:05 1001 error getting results 19:02 2 processing 19:15 1001

msg contains error: {'time': '19:02', 'duration': 2, 'msg': 'error getting results'}

[{'time': '18:05', 'duration': 1001, 'msg': 'processing'}, {'time': '19:15', 'duration': 1001, 'msg': 'processing'}, {'time': '18:00', 'duration': 123, 'msg': "i'm started"}, {'time': '19:02', 'duration': 2, 'msg': 'error getting results'}]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment