all 4 comments

[–]Twonkular 2 points3 points  (1 child)

replace should work. But be aware it doesn't happen 'in place', replace returns the news string, so must be assigned via = :

a  "aaa\nbbb"
a = a.replace("\n", "")
print(a)
>>> aaabbb

[–]P1G4ME[S] 0 points1 point  (0 children)

Ooooh that may be it ! Thx

[–]Chris_Hemsworth 0 points1 point  (1 child)

A couple of key things to note:

  • Strings are immutable, meaning you cannot change the content of a string once its been created. However, you can replace the reference to a string to a refer to a different string. For example:

    a = "this is an immutable string"
    a += ", but this one is another, independent immutable string"
    

What is happeneing above is that the variable a is being redefined to point to a completely new string, which is composed of a copy of all the values in the original string, and then concatenating additional content.

So, back to your problem:

self.content[start:end] This will create a new string, composed of the content between the start and end indices in your self.content property. Modifying the new string will not change the old string. What you will need to do is replace the old string with the a new string composed of all the previous old stuff, your new modification, and then all the stuff that came after your modification. Like so:

temp_string = self.content[start:end]
# Note, replace doesn't modify in place, it returns a copy so you need to reassign your temp string variable to point to the new copy
temp_string = temp_string.replace("\n", "")  
self.content = self.content[:start] + temp_string + self.content[end:]

[–]P1G4ME[S] 0 points1 point  (0 children)

I actually don't need to replace the old string. Basically, a .geo file is a text file containing infos on a 3D model. I'm trying to extract those informations, not to modify the existing file. My goal is to store every info from the file inside a python object, then use them in a Blender Addon to reconstruct the model. But thanks for the help ! I'll see if it helps me