I'm trying to change the |
symbol with the _
on a text of a file and overwrite the result.
The text of my file is something like:
#Hello there|this is my text_to_modify
So as you can see the _
is already present is some parts of the text but I want to change only the |
symbol.
The code I wrote is:
import re
with open(my_file,'r+') as f:
text = f.read()
text = re.sub('|', '_', text)
f.seek(0)
f.write(text)
f.truncate()
f.close()
and the output is:
#Hello_there|this_is_my_text_to_modify
What can I do? Thanks in advance for the answers.
You need to escape the pipe symbol:
text = re.sub('\|', '_', text)
|
has a special meaning in regular expressions ('or') and has to be escaped to be used literally. I don't understand how you got the output you reported, because what your regex '|'
is basically saying is, 'match nothing or nothing and replace it with an underscore', so your output should have underscores between all of the characters (where 'nothing' is).
I recommend you take a good look at the excellent Python regex documentation.
As a side note, the way you overwrite the original file is very dangerous: If you mess something up, you lose all the content from the original file. Better output into a new file.