-2

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

viGor027
  • 67
  • 6
  • If you need to replace, use `re.sub(pattern, "SAMPLE TEXT", text)`. If you need special replacement logic, use a callbable instead of a string. – Wiktor Stribiżew Feb 20 '21 at 17:17
  • @WiktorStribiżew so as a pattern I pass my pattern, sample text is my text after change, and text is text that I want to make changes in ? – viGor027 Feb 20 '21 at 17:19

3 Answers3

0

The problem is this line

text.replace(fields_to_replace[i], "SAMPLE TEXT")

Strings in python and most (if not all) other languages are immutable. text.replace returns a string. You would need to write something like

text = text.replace(fields_to_replace[i], "SAMPLE TEXT")
Kraigolas
  • 5,121
  • 3
  • 12
  • 37
0

In python text.replace() does not replace anything, but returns new string with replaced words.

you need:

text = text.relace(...)
user3431635
  • 372
  • 1
  • 7
0

you can use re.sub:

import re
text="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."


print(re.sub(r"<\w+>", "SAMPLE TEXT", text))

which out puts: Love at First Sight One SAMPLE TEXT afternoon, I was walking by the SAMPLE TEXT when accidentally I bumped into a SAMPLE TEXT boy. At first I blushed and apologized for bumping into him, but when he flashed his SAMPLE TEXT smile I just couldn’t help falling in love. His SAMPLE TEXT voice telling me that it was ok sounded like music to myears.