0

I have an log-file with some written f-strings. Now I want to get the parameters again and save them in a dictionary.

e.g. I have the following log:

The following data is received: Name: Kreijeck, age=35, country=Germany

from this method:

def create_fstring():
    name = "Kreijeck"
    age = "35"
    country = "Germany"

    print(f"The following data is received: Name: {name}, age={age}, country={country}")

Now I want to create a Method that can extract the parameters again:

def get_key_value_pairs(f_string, log_string) -> dict:
    ### PSEUDOCODE ###
    d = f_string.extract_parametes(log_string)
    return d

where d shall be:

d = {
    'name': "Kreijeck",
    'age': 35,
    'country': 'Germany',
    }

when the input is the correct f_string

    f_string  = "The following data is received: Name: {name}, age={age}, country={country}"
  • Either use something like regex or just use basic string splits over and over again, e.g split at Name:, at age=, .... or even better: use a proper structured data exchange format, e.g. json. – luk2302 Feb 06 '22 at 14:16
  • Thanks @luk2302, than I will try to use groups from regular expressions here: – Kreijeck Feb 06 '22 at 19:17

0 Answers0