I have a text and I have got a task in python with reading module:
Find the names of people who are referred to as Mr. XXX
. Save the result in a dictionary with the name as key and number of times it is used as value. For example:
- If Mr. Churchill is in the novel, then include
{'Churchill' : 2}
- If Mr. Frank Churchill is in the novel, then include
{'Frank Churchill' : 4}
The file is .txt and it contains around 10-15 paragraphs.
Do you have ideas about how can it be improved? (It gives me error after some words, I guess error happens due to the reason that one of the Mr.
is at the end of the line.)
orig_text= open('emma.txt', encoding = 'UTF-8')
lines= orig_text.readlines()[32:16267]
counts = dict()
for line in lines:
wordsdirty = line.split()
try:
print (wordsdirty[wordsdirty.index('Mr.') + 1])
except ValueError:
continue