使用Python编写换行符时,避免写回车符'\ r' [英] Avoid writing carriage return '\r' when writing line feed with Python

查看:209
本文介绍了使用Python编写换行符时,避免写回车符'\ r'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果考虑到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'

使用open('filename','w').write('text\n')时如何避免写回车符?

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

这表明只写了换行符,因此它应该是5个字节长.

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
                                  ^

它实际上是6个字节长.现在这可能是"Windows事物",但是,例如,当您在Notepad ++中打开文件,并打开查看>显示符号>显示所有字符时,您可以看到回车.

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.

按CTRL + H并使用扩展搜索模式将\ r替换为\ r后,仅保留换行符.保存文件后,文件中仅包含换行符,并且文件长为5个字节.

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
                              ^

那为什么Notepad ++可以保存换行而无需回车,而python不能呢?

So why is Notepad++ able to save line feeds without carriage return, but python can't?

推荐答案

打开文本文件时,可以通过将''传递给newline参数来实现此目的.

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()

文档中所述:

换行符控制通用换行符模式的工作方式(仅适用于 文字模式).可以是无",","\ n","\ r"和"\ r \ n".它作为 如下:

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:

  • 从流中读取输入时,如果换行符为None,则启用通用换行符模式.输入中的行可以以'\ n','\ r'或 '\ r \ n',并且在返回之前将其转换为'\ n' 呼叫者,召集者. 如果为",则表示启用了通用换行模式,但第 结束语将不翻译地返回给呼叫者.如果其中有一个 其他合法值,输入行仅由给定终止 字符串,并且行尾未经翻译返回给调用方.

  • 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.

将输出写入流时,如果换行符为None,则写入的任何'\ n'字符都将转换为系统默认行 分隔符,os.linesep.如果换行符是''或'\ n',则无需翻译 地方.如果换行符是其他任何合法值,则任何'\ n' 写入的字符将转换为给定的字符串.

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.

newline的默认值为None,通过指定'',您将强制Python编写换行符(\n\r)而不进行翻译.

The default value for newline is None, by specifiying '', you're forcing Python to write the newline (\n or \r) without translating it.

这篇关于使用Python编写换行符时,避免写回车符'\ r'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆