read file and replace substrings in python

Question:

I am trying to read a text file with python and remove certain strings from it.
I have written a script, but for some reason it does not do what I would expect.

As a minimal example I have the following text file named test.txt

Here some random text
with words I want to replace

and my python3 script

with open(r'test.txt',"r") as text_file:
    content = text_file.read()
    print("the content:")
    print(content)
    
    content.replace("Here","")
    print("The replaced content:")
    print(content)

The output is

The content:
Here some random text
with words I want to replace

The replaced content:
Here some random text
with words I want to replace

So you can see the replacement function did not work. However the following script works:

test_text = "Here some random textnwith words I want to replace"
print(test_text)
print(test_text.replace("Here", "Hello"))

the output is as expected

Here some random text
with words I want to replace
Hello some random text
with words I want to replace

When I tested the type of text_file.read() by type(text_file.read()) the output was <class 'str'>. Hence, I have no idea why my script does not work

Asked By: Nathanael Skrepek

||

Answers:

The string replace() method returns the string. You can fix this by replacing

content.replace("Here","")

with

content = content.replace("Here","")

This is also why the second print statement works, since you print the return statement of the replace method.

The reason is that a python str is immutable. This means that a string object cannot be mutated. Anything that suggests that it does an "operation" on such a string creates a new instance instead.

Answered By: DWe1

.replace() is not an in-place operation. See:

string = "text from a string"
print(string)
string.replace('from','in')
print(string)
string = string.replace('from','in')
print(string)

Outputs:

text from a string
text from a string
text in a string
Answered By: Tempman383838
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.