我如何在Python中指明字符串中的换行符,以便我可以将多行写入文本文件?


当前回答

各种等效方法

使用打印

默认情况下,打印已经追加了换行符!

with open("out.txt", "w") as f:
    print("First", file=f)
    print("Second", file=f)

等同于:

with open("out.txt", "w") as f:
    print("First\nSecond", file=f)

要打印而不自动添加换行符,使用sep=""(因为sep="\n"是默认值):

with open("out.txt", "w") as f:
    print("First\nSecond\n", sep="", file=f)

使用f.write

对于以文本模式打开的文件:

with open("out.txt", "w") as f:
    f.write("First\nSecond\n")

对于以二进制模式打开的文件,写入文件时不会自动将\n转换为特定于平台的行结束符。要强制使用当前平台的换行符,请使用os。Linesep代替\n:

with open("out.txt", "wb") as f:
    f.write("First" + os.linesep)
    f.write("Second" + os.linesep)

输出文件

视觉:

First
Second

在Linux上,换行符将以\n分隔:

First\nSecond\n

在Windows中,换行符将以\r\n分隔:

First\r\nSecond\r\n

为了避免以文本模式打开的文件自动将\n转换为\r\n,请使用open("out.txt", "w", newline="\n")打开文件。

其他回答

各种等效方法

使用打印

默认情况下,打印已经追加了换行符!

with open("out.txt", "w") as f:
    print("First", file=f)
    print("Second", file=f)

等同于:

with open("out.txt", "w") as f:
    print("First\nSecond", file=f)

要打印而不自动添加换行符,使用sep=""(因为sep="\n"是默认值):

with open("out.txt", "w") as f:
    print("First\nSecond\n", sep="", file=f)

使用f.write

对于以文本模式打开的文件:

with open("out.txt", "w") as f:
    f.write("First\nSecond\n")

对于以二进制模式打开的文件,写入文件时不会自动将\n转换为特定于平台的行结束符。要强制使用当前平台的换行符,请使用os。Linesep代替\n:

with open("out.txt", "wb") as f:
    f.write("First" + os.linesep)
    f.write("Second" + os.linesep)

输出文件

视觉:

First
Second

在Linux上,换行符将以\n分隔:

First\nSecond\n

在Windows中,换行符将以\r\n分隔:

First\r\nSecond\r\n

为了避免以文本模式打开的文件自动将\n转换为\r\n,请使用open("out.txt", "w", newline="\n")打开文件。

在Python 3中,该语言会在平台的原生表示中为你编码换行符。这意味着在Windows上是\r\n,而在成熟的系统上只是\n。

即使在U*x系统上,以文本模式读取Windows行结束符的文件也会返回正确的文本结果,即在\n字符之前的任何\r字符都会被无声地删除。

如果需要完全控制文件中的字节,则可以使用二进制模式。然后,每个字节恰好对应一个字节,Python不执行任何转换。

>>> # Write a file with different line endings, using binary mode for full control
>>> with open('/tmp/demo.txt', 'wb') as wf:
...     wf.write(b'DOS line\r\n')
...     wf.write(b'U*x line\n')
...     wf.write(b'no line')
10
9
7

>>> # Read the file as text
>>> with open('/tmp/demo.txt', 'r') as text:
...     for line in text:
...         print(line, end='')
DOS line
U*x line
no line

>>> # Or more demonstrably
>>> with open('/tmp/demo.txt', 'r') as text:
...     for line in text:
...         print(repr(line))
'DOS line\n'
'U*x line\n'
'no line'

>>> # Back to bytes!
>>> with open('/tmp/demo.txt', 'rb') as binary:
...     for line in binary:
...         print(line)
b'DOS line\r\n'
b'U*x line\n'
b'no line'

>>> # Open in binary, but convert back to text
>>> with open('/tmp/demo.txt', 'rb') as binary:
...     for line in binary:
...         print(line.decode('utf-8'), end='')
DOS line
U*x line
no line

>>> # Or again in more detail, with repr()
>>> with open('/tmp/demo.txt', 'rb') as binary:
...     for line in binary:
...         print(repr(line.decode('utf-8')))
'DOS line\r\n'
'U*x line\n'
'no line'

\n -简单换行符插入工作:

# Here's the test example - string with newline char:
In [36]: test_line = "Hi!!!\n testing first line.. \n testing second line.. \n and third line....."

输出:

In [37]: print(test_line)

Hi!!!
 testing first line..
 testing second line..
 and third line.....

这取决于你想要多正确。\n通常会做这项工作。如果你真的想要正确,你可以在os包中查找换行符。(这实际上叫做linesep。)

注意:当使用Python API写入文件时,不要使用os.linesep。只使用\n;Python会自动将其转换为适合您平台的换行符。

Use:

"{}\n{}\n{}".format(
    "line1",
    "line2",
    "line3"
)

我个人更喜欢这种形式。