the problem is that my script doesn't change anything.
Here is what I've tried already:
import re
f = open('loveatfirstsight.txt', 'r', encoding='utf-8')
#print(f.read())
def get_fields(directory):
pattern = re.compile(r'<\s*(.*?)\s*>')
results = []
with open(directory, 'r', encoding='utf-8') as f:
text = f.read()
matches = pattern.finditer(text)
for match in matches:
results.append(match[0])
print(results)
return results
def replace_fields(directory, fields_to_replace):
with open(directory, 'r', encoding='utf-8') as f:
text = f.read()
for i in range(len(fields_to_replace)):
text.replace(fields_to_replace[i], "SAMPLE TEXT")
print(text)
replace_fields('loveatfirstsight.txt', get_fields('loveatfirstsight.txt'))
My pattern r'<\s*(.*?)\s*>'
finds all occurencies of words that starts with "<" and ends with ">".
file loveatfirstsight.txt
looks like that:
Love at First Sight
One <adjective> afternoon, I was walking by the <place> when
accidentally I bumped into a <adjective> boy.
At first I blushed and apologized for bumping into him, but when he flashed his
<adjective> smile I just couldn’t help falling in love. His
<adjective> voice telling me that it was ok sounded like music to myears.
I could have stayed there staring at him for <period_of_time>.
He had <adjective> <color> eyes and <adjective>
<color> hair. I thought he was perfect for me. Before I noticed,
<number> <period_of_time> had passed by after I apologized,
and I hadn’t said anything else since!
That’s when I noticed thathe was looking at me
<adverb>. I didn’t know what tosay, so I just <past_verb>.
I noticed him giving me astrange look when he started walking to his
<noun>.I looked back at him <number> more time(s), but hewas already out of sight.
It wasn’t love after all
I want to have all words closed with "<>" to be replaced with "SAMPLE TEXT"
. Everything seems to be logic to me, but unfortunately it doesn't work.
Just to clarify - "SAMPLE TEXT"
will be changed to list that contains needed words(so we don't need to worry about it - I just want it to make it going and then I will handle it).
Thanks