简体   繁体   English

使用Python编写换行符时,避免使用回车符'\\ r'来编写

[英]Avoid writing carriage return '\r' when writing line feed with Python

If taken into consideration that carriage return = \\r and line feed = \\n 如果考虑到carriage return = \\rline 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') ? 使用open('filename','w').write('text\\n')时如何避免写回车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. 这表示只写入换行符,因此它应该是5个字节长。

-rw-r--r-- 1 djuric 197121        6 Jul 15 21:00 filename
                                  ^

It is actually 6 bytes long. 它实际上是6个字节长。 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. 现在这可以是“Windows的东西”,但是当您在Notepad ++中打开文件时,如果您打开视图>显示符号>显示所有字符,您可以看到回车符。

After pressing CTRL+H and replacing \\r with nothing using Extended Search Mode, only line feed is left. 按CTRL + H并使用扩展搜索模式替换\\ r时,只剩下换行符。 After saving the file, only line feed is in the file and the file is 5 bytes long. 保存文件后,只有换行符在文件中,文件长度为5个字节。

-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? 那么为什么Notepad ++能够在没有回车的情况下保存换行符,但是python不能?

You can do this by passing '' to the newline parameter when opening the text file. 您可以通过在打开文本文件时将''传递给newline参数来完成此操作。

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'. 它可以是None,'','\\ n','\\ r'和'\\ 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. 输入中的行可以以'\\ n','\\ r'或'\\ r \\ n'结尾,并且在返回给调用者之前将这些行转换为'\\ n'。 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. 将输出写入流时,如果换行为None,则写入的任何“\\ n”字符都将转换为系统默认行分隔符os.linesep。 If newline is '' or '\\n', no translation takes place. 如果换行符是''或'\\ n',则不会进行翻译。 If newline is any of the other legal values, any '\\n' characters written are translated to the given string. 如果换行符是任何其他合法值,则写入的任何“\\ n”字符都将转换为给定的字符串。

The default value for newline is None , by specifiying '' , you're forcing Python to write the newline ( \\n or \\r ) without translating it. newline的默认值为None ,通过指定'' ,您强制Python在不翻译的情况下编写换行符( \\n\\r )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM