I have this dict of titles:
TITLES = {"Male": ["mr "], "Female": ["mrs ", "ms "], "Neutral": ["dr ", "brigadier general "]}
And have this list of names:
Names = ["A/Prof John Stones", "Ms Jennifer Jetkins", "Aaron Green", "Dr Vega", "Osvaldo"]
I want to split the name on titles if it is in the name and be able to access the title part and name part. I wrote this:
def parse_name(raw_name):
match = re.search(r'^(w+s)(.*)$', raw_name, re.IGNORECASE)
if match.group(1).lower() in sum(TITLES.values(), []):
person_title = match.group(1).title()
person_name = match.group(2).title()
else:
person_title = ""
person_name = raw_name.strip().title()
return {"name": person_name, "title": person_title}
But I am getting an error:
'NoneType' object has no attribute 'group'
It seems to be working for names that have the title from TITLES. What would be a way to fix this so it returns title and name (from TITLES or empty string if not in TITLES)?