If taken into consideration that carriage return = \r
and line feed = \n
Python 3.5.1 (v3.5.1:37a07cee5969, Dec 6 2015, 01:38:48) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> '{:02x}'.format(ord('\n'))
'0a'
>>> '{:02x}'.format(ord('\r'))
'0d'
how to avoid writing carriage return when using open('filename','w').write('text\n')
?
In interactive mode you can do this:
>>> open('filename','w').write('text\n')
5
>>> for c in open('filename','r').read():
... print('{:02x}'.format(ord(c)))
...
74
65
78
74
0a
This would indicate that only line feed has been written, thus it should be 5 bytes long.
-rw-r--r-- 1 djuric 197121 6 Jul 15 21:00 filename
^
It is actually 6 bytes long. Now this can be a "Windows thing", but when you open the file in Notepad++ for example, and you turn View > Show Symbols > Show All Characters you can see the carriage return there.
After pressing CTRL+H and replacing \r with nothing using Extended Search Mode, only line feed is left. After saving the file, only line feed is in the file and the file is 5 bytes long.
-rw-r--r-- 1 djuric 197121 5 Jul 15 20:58 filename1
^
So why is Notepad++ able to save line feeds without carriage return, but python can't?
Depending on the type of file (and the OS it comes from, etc), your carriage return might be '\r' , '\n' , or '\r'\n' . The best way to get rid of them regardless of which one they are is to use line. rstrip() .
\r takes the cursor to the beginning of the line. It is the same effect as in a physical typewriter when you move your carriage to the beginning and overwrite whatever is there.
Newline character in Python: In Python, the new line character “\n” is used to create a new line. When inserted in a string all the characters after the character are added to a new line.
Conceptually, \r moves the cursor to the beginning of the line and then keeps outputting characters as normal. You also need to tell print not to automatically put a newline character at the end of the string. In python3, you can use end="" as in this previous stackoverflow answer.
You can do this by passing ''
to the newline
parameter when opening the text file.
f = open('test.txt', 'w', newline='')
f.write('Only LF\n')
f.write('CR + LF\r\n')
f.write('Only CR\r')
f.write('Nothing')
f.close()
As described in the docs:
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.
The default value for newline
is None
, by specifiying ''
, you're forcing Python to write the newline (\n
or \r
) without translating it.
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