简体   繁体   English

打印多行,行之间没有空格

[英]Print multiple lines without space between lines

I have a list of lines (from a text file) to be printed in terminal. 我有要在终端中打印的行列表(来自文本文件)。 This is the format I like (without empty lines between printed lines): 这是我喜欢的格式(打印行之间没有空行):

>>> print("line \n" * 5)
line 
line 
line 
line 
line 

This is how it looks (a line with text, an empty line, etc.): 这是它的外观(带有文本的行,空行等):

>>> for x in range(0, 5): print "line \n"
... 
line 

line 

line 

line 

line

Any idea why it's working in this way and how to print my list of lines without empty lines between them? 知道为什么它会以这种方式工作以及如何在行之间没有空行的情况下打印行列表吗?

UPDATE (this is where I want to get rid of empty lines): 更新(这是我要摆脱空行的地方):

with open(FILE, 'r') as f:
    lines = f.readlines()
    for line in lines:
        print line

使用for循环时,可以如下所示删除“ \\ n”

for x in range(0, 5): print "line"

The print command implicitly adds a new line feed \\n in the output display. 打印命令在输出显示中隐式添加一个新的换行符\\n Remove the \\n to avoid printing blank lines in the looping structure. 删除\\n以避免在循环结构中打印空白行。

print adds a trailing newline by default. print默认情况下会添加尾随换行符。 You can disable it as follows: 您可以按以下方式禁用它:

from __future__ import print_function  # only needed for python 2
print("string", end="")

This is actually due to the way you're reading the file . 这实际上是由于您读取文件的方式 If you've done something like 如果您做了类似的事情

with open(FILE) as f:
    data = f.readlines()

Then the data will be a list of lines which retain the end of line character. 然后, data将是保留行尾字符的行列表。 If you don't want the end of line character, use this instead: 如果您不希望行尾字符,请改用以下命令:

with open(FILE) as f:
    data = f.read().splitlines()

If you don't want to change the way that you read the data into a list, then concatenate and print the lines like this instead: 如果您不想更改将数据读取到列表中的方式,请串联并打印如下行:

print ''.join(data)

To answer the literal question, if you have a string with a newline, and you want to suppress the newline generated by the print statement, the way to do that in Python 2 is by using a trailing comma on the print statement: 要回答字面问题,如果您有一个带有换行符的字符串,并且想隐藏由print语句生成的换行符,那么在Python 2中做到这一点的方法是在print语句上使用一个逗号结尾:

with open(FILE) as f:
    for line in f:
        print line,

But note, that's not really necessary to iterate line by line like that. 但是请注意,像这样逐行迭代实际上并没有必要。 You may as well just print the data directly: 您也可以直接打印数据:

with open(FILE) as f:
    print f.read()

By default print adds a newline, and your strings also have it; 默认情况下, print添加一个换行符,您的字符串也有换行符; you have two possible solutions. 您有两种可能的解决方案。

Either override this behaviour, setting the string to be put to empty: 要么覆盖此行为,要么将字符串设置为空:

# python 3.*
for x in range(0, 5): print("line \n", end="")

And

# python 2.*
from __future__ import print_function  # to use py3 print in py2
for x in range(0, 5): print("line \n", end="")

Alternatively, if you don't have full control over the lines - some may have the newlines, other - not, trim any newline endings and leave the default behaviour: 或者,如果您不能完全控制行-有些行可能拥有换行符,而其他人-则不修剪任何换行符结尾并保留默认行为:

for x in range(0, 5): print("line \n".rstrip("\n").rstrip("\r"))  # takes care of also Windows line ending, just in case

A standard call to the print function will add a newline automatically so you can either remove the \\n from your existing line(s), if the lines come from an external file use line.strip() , or tell print not to add the newline with: print(line, end='') 标准调用print函数将自动添加换行符,因此, 如果这些行来自外部文件可以从现有行中删除\\n ,请使用line.strip() ,或告诉print不要添加换行符: print(line, end='')

This will work for Python3 or for 2.7 with from __future__ import print_function as the first code line. 这将适用于Python3或2.7, from __future__ import print_function作为第一行代码。

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

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