N
N
nik192021-12-15 10:29:30
Python
nik19, 2021-12-15 10:29:30

Replacing text in a file. replace() function in python not working for file?

I've been reading a lot of answers but still can't figure it out. Seems like it should work.
The text file contains unique "variables" (text strings) like "abb1" , "abb2", "abb3"...
They occur only once each. The file is relatively small - 2 MB/4000 lines.
Should I be sure to read the file line by line? From various articles and answers - should not. You can read the entire file and use the replace() function for the entire file at once. Moreover, she has a parameter for the number of replacements.
But in practice, nothing will change.

abb = "abb1"
result = "1234"
with open("f1.svg", 'r') as f1, open("f2.svg", 'w') as f2:
            f1read= f1.read()
            f1read = f1read.replace(abb, result)
            f2.write(f1read)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question