2

I need to make a dictionary list like this (match the owner with their pet type) from log.txt

dictionary = {
  "Sophia": "Cat",
  "Julia": "Bird",
  "Max": "Dog"
}

log.txt

Pet Owner : Sophia
    Colour : Blue
    Pet Type : Cat
    From : India
    Price : High

Pet Owner : Bruce
    Not own pet

Pet Owner : Sean
    Not own pet

Pet Owner : Julia
    Colour : Yellow
    Pet Type : Bird
    From : Israel
    Price : Low

Pet Owner : Bean
    Not own pet

Pet Owner : Max
    Colour : Green
    Pet Type : Dog
    From : Italy
    Price : Normal

Pet Owner : Clarie
    Not own pet

What I have try so far

import re

log = open("log.txt", "r")
txt = log.read()
log.close()

x = re.search("^Pet.Owner.+", txt)
print(x.group())

I stuck at here, I don't know how to make the regEx return 2 keyword that I wanted and save it to the dictionary.txt.

  • You can use this `(?:Pet Owner|Pet Type) : ([^\n]+)` pattern and than build object using chunks of two at time from obtained result – Code Maniac Aug 13 '19 at 02:23
  • @CodeManiac like this? `x = re.search("(?:Pet Owner|Pet Type) : ([^\n]+)", txt)` ? it give output `Pet Owner : Sophia` what does that mean? – Izzuddin Jamaluddin Aug 13 '19 at 02:41
  • @IzzuddinJamaluddin you should look into this https://stackoverflow.com/questions/52603287/return-multiple-matches-using-re-match-or-re-search – bkyada Aug 13 '19 at 04:53

1 Answers1

2

You can use tempered greedy token, see regex101:

s = '''Pet Owner : Sophia
    Colour : Blue
    Pet Type : Cat
    From : India
    Price : High

Pet Owner : Bruce
    Not own pet

Pet Owner : Sean
    Not own pet

Pet Owner : Julia
    Colour : Yellow
    Pet Type : Bird
    From : Israel
    Price : Low

Pet Owner : Bean
    Not own pet

Pet Owner : Max
    Colour : Green
    Pet Type : Dog
    From : Italy
    Price : Normal

Pet Owner : Clarie
    Not own pet'''

import re

out = dict( re.findall(r'Pet Owner : (\w+)(?:(?!Pet Owner :).)*Pet Type : (\w+)', s, flags=re.DOTALL) )

print(out)

Prints:

{'Sophia': 'Cat', 'Julia': 'Bird', 'Max': 'Dog'}
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91