Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove ^M

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)
like image 404
DGT Avatar asked Jul 07 '10 01:07

DGT


People also ask

What is Ctrl-M character?

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.

What is the M character?

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.

How do I remove special characters from a text file in Linux?

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.


1 Answers

string.replace('\r', '') worked for me.

Ugly, but nor r+ nor r+b nor NOTHING ELSE worked (for me, sure) :(

like image 164
inigoD Avatar answered Sep 30 '22 20:09

inigoD