Avoid writing carriage return when writing line fe

2019-04-28 13:32发布

问题:

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?

回答1:

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.