Python - How do I update a dictionary with a loop that has one key and two values?

AVilenius

I'm doing an exercise but I'm a bit stuck. I need to parse through a text file that has ERROR or INFO messages attached to a username. I then need to create a dictionary where I have the username as the key and one value being the amount of ERROR messages, and the other value being the amount of INFO messages. I will then put that in a CSV file where I need the header for the key to be "Username" and the other two headers be "ERROR" and "INFO"

Basically the finished result needs to look like this:

enter image description here

The part I'm stuck at is the first bit where I need to update a dictionary with the username as key and the amount of messages as values.

This is the code I have so far where I've only been able to figure out how to grab the amount of messages for the username but they're in two separate dictionaries:

import re
import csv
from collections import Counter

test_list = []
test_list2 = []


with open(r"syslog.txt", "r") as log:
  for i in log:
    if re.findall("ERROR.*", i):
      test_list.append(re.findall("ticky:.*ERROR [\w '].*\(([\w\.]*).*$", i))
    elif re.findall("INFO.*", i):
      test_list2.append(re.findall("ticky:.*INFO [\w '].*\(([\w\.]*).*$", i))

flattened = [val for sublist in test_list for val in sublist]
test_dict = Counter(flattened)
print(test_dict)

flattened2 = [val for sublist in test_list2 for val in sublist]
test_dict2 = Counter(flattened2)
print(test_dict2)

And here's a few lines from the syslog.txt:

Jan 31 05:18:45 ubuntu.local ticky: ERROR Tried to add information to closed ticket (sri)
Jan 31 05:23:14 ubuntu.local ticky: INFO Commented on ticket [#1097] (breee)
Jan 31 05:35:00 ubuntu.local ticky: ERROR Connection to DB failed (nonummy)
Jan 31 05:45:30 ubuntu.local ticky: INFO Created ticket [#7115] (noel)
Jan 31 05:51:30 ubuntu.local ticky: ERROR The ticket was modified while updating (flavia)

So now I need to figure out a way to get these values in the same dictionary while keeping the key. Preferably I want to do it without merging the two dictionaries and instead just create one dictionary from the start.

Any help is very much appreciated!

Jorge Mauro

You could use a dictionary in order to store other two dictionaries

info = dict()
error = dict()
username = {'info': info, 'error': error}
users = {'username': username}

How to get values?

for user in users:
    print(user['info'])
    print(user['error'])

You could create a user object

class User:
    def __init__(self, username, info, error):
        self.username = username
        self.info = info
        self.error = error
    

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I add values to just one key in a dictionary in python?

How do I append multiple lists to one key in a python dictionary?

How to output one value from a dictionary whose key has two values

How do I update only values in a dictionary?

How do I combine values of two lists as one in Python?

C# - how to write a collection (Dictionary?) with one key and two values

Python Update Dictionary Key with Dictionary Values

How do I sort dictionary by Key in Python

python-iterating dictionary values if one key has one or more values

How can I access values from a key that stores more than one value in a dictionary in python?

How do I loop through all values in a dictionary if the values are dates

How can I sort dictionary values by one key?

How do I check if a dictionary has a key in it in Julia?

python: Dictionary key as row index, values as column headers. How do I refer back and select specific values in a df using a dictionary?

How do I update a dictionary where the key is a variable?

How can I compare two different values(array) in one dictionary?

Python: How to store multiple values for one dictionary key

How to make a dataframe with one key to multiple list values to a dictionary in python?

bash loop over two files and one file has two key values

How do I counter value in a for loop as part of my dictionary key

PYTHON - How do I sort and round the values of a dictionary but keep them assigned to the same key?

How do i replace values of a list when found in the dictionary with its key? python

How do i sort a dictionary by value that has tuples as values

I have two list and I want to create a dictionary from it . But I want to store multiple values for one key

How do I loop over dictionary and check for values passed by a variable in Python

Python: How do I format a loop to pull specific values in a dictionary list?

How do I add dictionary individual key values together?

How do I add multiple values to a key dictionary

How do I sort a key:list dictionary by values in list?