Python 正在向输出添加额外的换行符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18028504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:46:02  来源:igfitidea点击:

Python is adding extra newline to the output

python

提问by

The input file: a.txt

输入文件: a.txt

aaaaaaaaaaaa
bbbbbbbbbbb
cccccccccccc

The python code:

蟒蛇代码:

with open("a.txt") as f:
    for line in f:
        print line

The problem:

问题:

[root@a1 0]# python read_lines.wsgi
aaaaaaaaaaaa

bbbbbbbbbbb

cccccccccccc

as you can see the output has extra line between each item.

如您所见,输出在每个项目之间都有额外的行。

How to prevent this?

如何防止这种情况?

采纳答案by Eric O Lebigot

printappends a newline, and the input lines already end with a newline.

print追加换行符,并且输入行已经以换行符结尾。

A standard solution is to output the input lines verbatim:

标准解决方案是逐字输出输入行:

import sys

with open("a.txt") as f:
    for line in f:
        sys.stdout.write(line)

PS: For Python?3 (or Python?2 with the print function), abarnert's print(…, end='')solution is the simplest one.

PS:对于Python?3(或者带有print函数的Python?2),abarnert的print(…, end='')解决方案是最简单的。

回答by Hyperboreus

It is not adding a newline, but each scanned line from your file has a trailing one.

它不是添加换行符,而是从文件中扫描的每一行都有一个尾随行。

Try:

尝试:

with open ("a.txt") as f:
    for line in (x.rstrip ('\n') for x in f):
        print line

回答by jh314

What happens is that each line as a newline at the end, and printstatement in python also adds a newline. You can strip the newlines:

发生的情况是每一行最后都作为换行符,printpython 中的语句也添加了一个换行符。您可以去除换行符:

with open("a.txt") as f:
    for line in f:
        print line.strip()

回答by yelsayed

You could also try the splitlines()function, it strips automatically:

你也可以试试这个splitlines()功能,它会自动剥离:

f = open('a.txt').read()
for l in f.splitlines():
    print l

回答by abarnert

As the other answers explain, each line has a newline; when you printa bare string, it adds a line at the end. There are two ways around this; everything else is a variation on the same two ideas.

正如其他答案所解释的那样,每一行都有一个换行符;当您print使用裸字符串时,它会在末尾添加一行。有两种方法可以解决这个问题;其他一切都是相同的两个想法的变体。



First, you can strip the newlines as you read them:

首先,您可以在阅读时去除换行符:

with open("a.txt") as f:
    for line in f:
        print line.rstrip()

This will strip any other trailing whitespace, like spaces or tabs, as well as the newline. Usually you don't care about this. If you do, you probably want to use universal newline mode, and strip off the newlines:

这将删除任何其他尾随空格,如空格或制表符,以及换行符。通常你不会关心这个。如果这样做,您可能想使用通用换行符模式,并去掉换行符:

with open("a.txt", "rU") as f:
    for line in f:
        print line.rstrip('\n')

However, if you know the text file will be, say, a Windows-newline file, or a native-to-whichever-platform-I'm-running-on-right-now-newline file, you can strip the appropriate endings explicitly:

但是,如果您知道文本文件将是一个 Windows 换行文件,或者一个原生到任何平台的文件,您可以去掉适当的结尾明确地:

with open("a.txt") as f:
    for line in f:
        print line.rstrip('\r\n')

with open("a.txt") as f:
    for line in f:
        print line.rstrip(os.linesep)


The other way to do it is to leave the original newline, and just avoid printing an extra one. While you can do this by writing to sys.stdoutwith sys.stdout.write(line), you can also do it from printitself.

另一种方法是保留原来的换行符,避免打印额外的换行符。虽然您可以通过写入sys.stdoutwith来做到这一点,但您也可以通过sys.stdout.write(line)print本身来做到这一点。

If you just add a comma to the end of the printstatement, instead of printing a newline, it adds a "smart space". Exactly what that meansis a bit tricky, but the idea is supposed to be that it adds a space when it should, and nothing when it shouldn't. Like most DWIM algorithms, it doesn't always get things right—but in this case, it does:

如果你只是在print语句的末尾添加一个逗号,而不是打印换行符,它会添加一个“智能空格”。确切地说,这意味着什么有点棘手,但这个想法应该是它应该添加一个空间,而不应该添加一个空间。像大多数 DWIM 算法一样,它并不总是正确的——但在这种情况下,它确实:

with open("a.txt") as f:
    for line in f:
        print line,

Of course we're now assuming that the file's newlines match your terminal's—if you try this with, say, classic Mac files on a Unix terminal, you'll end up with each line printing over the last one. Again, you can get around that by using universal newlines.

当然,我们现在假设文件的换行符与您终端的换行符匹配——如果您在 Unix 终端上对经典 Mac 文件进行尝试,您最终会在最后一行打印每一行。同样,您可以通过使用通用换行符来解决这个问题。

Anyway, you can avoid the DWIM magic of smart space by using the print function instead of the print statement. In Python 2.x, you get this by using a __future__declaration:

无论如何,您可以通过使用 print 函数而不是 print 语句来避免智能空间的 DWIM 魔力。在 Python 2.x 中,您可以通过使用__future__声明来获得它:

from __future__ import print_function
with open("a.txt") as f:
    for line in f:
        print(line, end='')

Or you can use a third-party wrapper library like six, if you prefer.

或者six,如果您愿意,也可以使用第三方包装器库,例如。