0
import re, os

def replace_one_line_content_with_other(input_text):
    word_to_replace, replacement_word = "", ""                                                                            

    if (os.path.isfile('data_association/names.txt')):
        word_file_path = 'data_association/names.txt'
    else:
        open('data_association/names.txt', "w")
        word_file_path = 'data_association/names.txt'                                                                        

    #name_capture_pattern = r"([A-Z][\wí]+\s*(?i:del|de\s*el|de)\s*[A-Z]\w+)"
    name_capture_pattern = r"((?:\w+))?"
    regex_pattern = r"(?i:no\s*es)\s*" + name_capture_pattern + r"\s*(?i:sino\s*que\s*es)\s*" + name_capture_pattern
 
    n0 = re.search(regex_pattern, input_text)

    if n0: 
        word_to_replace, replacement_word = n0.groups()
        print(repr(word_to_replace))  # --> word that I need identify in the txt
        print(repr(replacement_word)) # --> word by which I will replace the previous one

        #After reaching this point, the algorithm will already have both words, both the one that must be replaced (in case it is identified within the txt) and the one with which it will be replaced.

        numero = None
        with open(word_file_path) as f:
            lineas = [linea.strip() for linea in f.readlines()]
            #Find the word in the .txt file where the words are stored
            if word_to_replace in lineas: numero = lineas.index(word)+1
            #REPLACE   word_to_replace  with  replacement_word  in this .txt line

input_text = "No es Marín sino que es Marina"
replace_one_line_content_with_other(input_text)

After identifying the string stored in the word_to_replace = 'Marín' variable inside the .txt file, I must replace it with the string that is inside the replacement_word = 'Marina' variable.

Original content in the .txt file (it must be assumed that we do not know which line the word is on, so the line number could not be indicated)

Lucy
Martin
Lucila
Samuel
Katherine
María del Pilar
Maríne
Marín
Steve
Robert

And the .txt content after the modification:

Lucy
Martin
Lucila
Samuel
Katherine
María del Pilar
Maríne
Marina
Steve
Robert

1 Answer 1

1

You can use the sub method of the regex module:

word_to_replace = "Hello"
replacement_word = "Bye"

with open('names.txt', 'r') as names_file_read:
    names = re.sub(word_to_replace, replacement_word, names_file_read.read())

with open('names.txt', 'w') as names_file_write:
    names_file_write.write(names)

This reads every line of the .txt file - and replaces word_to_replace with replacement_word - though not so efficient because we read, delete, and write everything back into place. If your .txt is small it should be good enough. Else, we might want to do the changes only on these specific words in the text, seeking to their position and changing them. I suppose you can use file.readlines() and file.seek() methods.

Sign up to request clarification or add additional context in comments.

2 Comments

Really thanks for the help. If I were to use .seek() would I have to use .readlines() as I did in the question's code, and then put a .seek() indicating the line number where the match occurred?
yes, but seek gets as an input the number of bytes to seek in, so you need to calculate how many bytes ahead of the beginning you are and is a real headache, I would prefer the "ugly-easy" way I wrote, but your choice :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.