How can I remove the ^M
character from a text file (at the end of line) in a Python script?
I did the following, and there are ^M
at every line-break.
file = open(filename, "w")
file.write(something)
It is known as carriage return. If you're using vim you can enter insert mode and type CTRL - v CTRL - m . That ^M is the keyboard equivalent to \r.
M is a codename held by a number of fictional characters in Ian Fleming's James Bond book and film series; the characters are the current or past heads of the Secret Intelligence Service—also known as MI6.
For this, we have written command that will delete “#@” and “%*” from lines 2 and 3 of “newfile. txt” respectively. The sed command used in above methods will display the result only on the terminal rather than applying the changes in the text file: for that, we must use the “-i” option of sed command.
string.replace('\r', '') worked for me.
Ugly, but nor r+ nor r+b nor NOTHING ELSE worked (for me, sure) :(
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With