python:将打印与上一打印行连接

python:将打印与上一打印行连接,python,Python,在python(2.6)中,是否可以将打印输出与前一行打印输出“连接”起来?尾随逗号语法(print x,)不起作用,因为大多数输出都应该有新行 for fc in fcs: count = getCount(fc) print '%s records in %s' % ('{0:>9}'.format(count),fc) if count[0] == '0': delete(fc) print '==> %s removed

在python(2.6)中,是否可以将打印输出与前一行打印输出“连接”起来?尾随逗号语法(
print x,
)不起作用,因为大多数输出都应该有新行

for fc in fcs:
    count = getCount(fc)
    print '%s records in %s' % ('{0:>9}'.format(count),fc)
    if count[0] == '0':
        delete(fc)
        print '==> %s removed' % (fc)
当前控制台输出:

     3875 records in Aaa
     3875 records in Bbb
        0 records in Ccc
==> Ccc removed
    68675 records in Ddd
预期结果:

     3875 records in Aaa
     3875 records in Bbb
        0 records in Ccc ==> Ccc removed
    68675 records in Ddd

以下方面应起作用:

for fc in fcs:
    count = getCount(fc)
    print '%s records in %s' % ('{0:>9}'.format(count),fc),
    if count[0] == '0':
        delete(fc)
        print '==> %s removed' % (fc)
    else:
        print ''

没有一个很好的方法可以缩短使用
delete()
来保持可读性的时间。

以下方法应该有效:

for fc in fcs:
    count = getCount(fc)
    print '%s records in %s' % ('{0:>9}'.format(count),fc),
    if count[0] == '0':
        delete(fc)
        print '==> %s removed' % (fc)
    else:
        print ''

没有一个很好的方法来缩短这段时间,即使用
delete()
来保持可读性。

您在询问print语句是否可以从前一行末尾删除换行符。答案是否定的

但你可以写:

if count[0] == '0':
    removed = ' ==> %s removed' % (fc)
else:
    removed = ''
print '%s records in %s%s' % ('{0:>9}'.format(count), fc, removed)

您询问print语句是否可以从前一行末尾删除换行符。答案是否定的

但你可以写:

if count[0] == '0':
    removed = ' ==> %s removed' % (fc)
else:
    removed = ''
print '%s records in %s%s' % ('{0:>9}'.format(count), fc, removed)
打印写入应用程序标准输出并添加换行符

但是,sys.stdout已经是指向同一位置的文件对象,并且文件对象的write()函数不会自动向输出字符串追加换行符,因此它应该是您想要的

打印写入应用程序标准输出并添加换行符


但是,sys.stdout已经是一个指向同一位置的文件对象,文件对象的write()函数不会自动向输出字符串追加换行符,因此它应该完全符合您的需要。

虽然Python 2没有您需要的功能,但Python 3有

所以你可以

from __future__ import print_function

special_ending = '==> %s removed\n' % (fc)
ending = special_ending if special_case else "\n"

print('%s records in %s' % ('{0:>9}'.format(count),fc), end=ending)

虽然Python2没有您想要的功能,但Python3有

所以你可以

from __future__ import print_function

special_ending = '==> %s removed\n' % (fc)
ending = special_ending if special_case else "\n"

print('%s records in %s' % ('{0:>9}'.format(count),fc), end=ending)

谢谢你的及时回复。在代码示例中,我省略了
delete(fc)
行,因此您的答案不再有效(我想?)。哦,您完全正确,我略读了问题,并将重点放在了打印方面,很抱歉我将进行编辑。谢谢Andrew。我接受了汤姆的回答,因为我首先明白了它是如何工作的。我很感激知道通往同一目的地的另一条路线。谢谢您的及时回复。在代码示例中,我省略了
delete(fc)
行,因此您的答案不再有效(我想?)。哦,您完全正确,我略读了问题,并将重点放在了打印方面,很抱歉我将进行编辑。谢谢Andrew。我接受了汤姆的回答,因为我首先明白了它是如何工作的。我很感激知道通往同一目的的另一条路线。谢谢你让我看看我以后升级时需要做什么。也许不是我写过的最好的python代码,但我希望你能理解:)谢谢你让我看看我以后升级时需要做什么。也许不是我写过的最好的python代码,但我希望你能明白:)你在“locatino”中翻动了你的“n”和“o”。。。或者是你?你在“locatino”中翻了n和o。。。还是你?