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)
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)
If you're writing the file, you should specify open(filename, "wb")
. That way, you'll be writing in binary mode, and Python won't attempt to determine the correct newlines for the system you're on.
Python can open a file in binary mode or in text mode. Text is the default, so a mode of "w" means write in text mode. In text mode, Python will adjust the line endings for the platform you're on. This means on Windows, this code:
f = open("foo.txt", "w")
f.write("Hello\n")
will result in a text file containing "Hello\r\n".
You can open the file in binary mode by using "b" in the mode:
f = open("foo.txt", "wb")
f.write("Hello\n")
results in a text file containing "Hello\n".
string.replace('\r', '') worked for me.
Ugly, but nor r+ nor r+b nor NOTHING ELSE worked (for me, sure) :(
For portability, you can try the following
import os
file = open(filename, "w")
file.write(something.replace('\r\n', os.linesep))
How to explicitly set carriage return when doing json.dump? contains the solution: the open
function has an extra parameter newline
:
file = open(filename, "w", newline="\n")
file.write(something)
newline controls how universal newlines mode works (it only applies to text mode). It can be None, '', '\n', '\r', and '\r\n'. It works as follows:
When reading input from the stream, if newline is None, universal newlines mode is enabled. Lines in the input can end in '\n', '\r', or '\r\n', and these are translated into '\n' before being returned to the caller. If it is '', universal newlines mode is enabled, but line endings are returned to the caller untranslated. If it has any of the other legal values, input lines are only terminated by the given string, and the line ending is returned to the caller untranslated.
When writing output to the stream, if newline is None, any '\n' characters written are translated to the system default line separator, os.linesep. If newline is '' or '\n', no translation takes place. If newline is any of the other legal values, any '\n' characters written are translated to the given string.
run autopep8 on the file
> apt-get install python-autopep8
> autopep8 python_file_name > new_python_file_name.py