Skip to main content
 首页 » 编程设计

python:将打印与上一个打印行连接起来

2025年05月04日12pengyingh

在 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 

请您参考如下方法:

import sys 
sys.stdout.write("hello world") 

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

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