3 回答
TA贡献1757条经验 获得超8个赞
这取决于您想做的正确程度。\n
通常会做的工作。如果您确实想解决问题,请在os
包中查找换行符。(实际上称为linesep
。)
注意:使用Python API写入文件时,请勿使用os.linesep
。随便用\n
; Python会自动将其转换为适合您平台的换行符。
TA贡献1818条经验 获得超8个赞
新行字符为\n。它在字符串内使用。
例:
print 'First line \n Second line'
\n换行符在哪里。
这将产生结果:
First line
Second line
TA贡献1995条经验 获得超2个赞
您可以分别在新行中编写,也可以在单个字符串中编写,这更容易。
例子1
输入项
line1 = "hello how are you"
line2 = "I am testing the new line escape sequence"
line3 = "this seems to work"
您可以单独编写“ \ n”:
file.write(line1)
file.write("\n")
file.write(line2)
file.write("\n")
file.write(line3)
file.write("\n")
输出量
hello how are you
I am testing the new line escape sequence
this seems to work
例子2
输入项
正如其他人在前面的答案中指出的那样,将\ n放在字符串中的相关点上:
line = "hello how are you\nI am testing the new line escape sequence\nthis seems to work"
file.write(line)
输出量
hello how are you
I am testing the new line escape sequence
this seems to work
添加回答
举报