Fab*_*ook 280 python line-breaks file-writing
我如何在python中指定一个新行?
要在String中与Java进行比较,您可以执行类似"First Line\r \nSecond Line"的操作
那你将如何在Python中做到这一点?用于保存包含多行的文件.
Cha*_*tin 320
取决于你想要的正确程度.\n
通常会做这个工作.如果你真的想要把它弄好,你可以在os
包中查找换行符.(它实际上被称为linesep
.)
注意:使用Python api写入文件时,请不要使用os.linesep
.只需使用\n
,Python会自动将其转换为适合您平台的换行符.
小智 49
新行字符是\n
.它在字符串中使用.
例:
print 'First line \n Second line'
Run Code Online (Sandbox Code Playgroud)
\n
换行符在哪里.
这会产生结果:
First line
Second line
Run Code Online (Sandbox Code Playgroud)
Hom*_*ani 20
您可以单独或在单个字符串中写入新行,这更容易.
line1 = "hello how are you"
line2 = "I am testing the new line escape sequence"
line3 = "this seems to work"
Run Code Online (Sandbox Code Playgroud)
你可以单独写'\n'
file.write(line1)
file.write("\n")
file.write(line2)
file.write("\n")
file.write(line3)
file.write("\n")
Run Code Online (Sandbox Code Playgroud)
hello how are you
I am testing the new line escape sequence
this seems to work
Run Code Online (Sandbox Code Playgroud)
正如其他人在上面指出的那样,将\n放在字符串中的相关位置:
line = "hello how are you\nI am testing the new line escape sequence\nthis seems to work"
file.write(line)
Run Code Online (Sandbox Code Playgroud)
hello how are you
I am testing the new line escape sequence
this seems to work
Run Code Online (Sandbox Code Playgroud)
Ban*_*nta 15
独立于平台的换行器:Linux、Windows 和 IOS
import os
keyword = 'physical'+ os.linesep + 'distancing'
print(keyword)
Run Code Online (Sandbox Code Playgroud)
输出:
physical
distancing
Run Code Online (Sandbox Code Playgroud)
Rea*_*aas 10
正如其他答案中提到的:“新行字符是 \n。它在字符串内部使用”。
我发现最简单易读的方法是使用“format”函数,使用 nl 作为新行的名称,并将要打印的字符串分解为要打印的确切格式:
Python 2:
print("line1{nl}"
"line2{nl}"
"line3".format(nl="\n"))
Run Code Online (Sandbox Code Playgroud)
Python 3:
nl = "\n"
print(f"line1{nl}"
f"line2{nl}"
f"line3")
Run Code Online (Sandbox Code Playgroud)
这将输出:
line1
line2
line3
Run Code Online (Sandbox Code Playgroud)
这样它就可以执行任务,并且还可以提高代码的可读性:)
如果您一次输入几行文本,我发现这是最易读的格式.
file.write("\
Life's but a walking shadow, a poor player\n\
That struts and frets his hour upon the stage\n\
And then is heard no more: it is a tale\n\
Told by an idiot, full of sound and fury,\n\
Signifying nothing.\n\
")
Run Code Online (Sandbox Code Playgroud)
每行末尾的\转义新行(这会导致错误).
如果仅print
不带任何参数的情况下调用,它将输出空白行。
print
Run Code Online (Sandbox Code Playgroud)
您可以将输出通过管道传输到这样的文件中(考虑您的示例):
f = open('out.txt', 'w')
print 'First line' >> f
print >> f
print 'Second line' >> f
f.close()
Run Code Online (Sandbox Code Playgroud)
它不仅与操作系统无关(甚至无需使用os
程序包),而且比放在\n
字符串中更具可读性。
该print()
函数在字符串的末尾有一个可选的关键字参数,称为end
,默认为OS的换行符,例如。\n
。因此,当您打电话时print('hello')
,Python实际上正在打印'hello' + '\n'
。这意味着当您print
不带任何参数调用时,它实际上是print '' + '\n'
,这导致换行符。
使用多行字符串。
s = """First line
Second line
Third line"""
f = open('out.txt', 'w')
print s >> f
f.close()
Run Code Online (Sandbox Code Playgroud)
值得注意的是,当您使用交互式 Python shell 或Jupyter Notebook检查字符串时,\n
和其他反斜杠字符串会按字面\t
意思呈现:
>>> gotcha = 'Here is some random message...'
>>> gotcha += '\nAdditional content:\n\t{}'.format('Yet even more great stuff!')
>>> gotcha
'Here is some random message...\nAdditional content:\n\tYet even more great stuff!'
Run Code Online (Sandbox Code Playgroud)
仅当打印或写入文件时,换行符、制表符和其他特殊的非打印字符才会呈现为空格:
>>> print('{}'.format(gotcha))
Here is some random message...
Additional content:
Yet even more great stuff!
Run Code Online (Sandbox Code Playgroud)
\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....."
Run Code Online (Sandbox Code Playgroud)
# 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....."
Run Code Online (Sandbox Code Playgroud)