【问题标题】:python print out is not in the same linepython打印出来不在同一行
【发布时间】:2013-07-08 15:27:08
【问题描述】:

我有疑问,当我尝试在同一行中用 python 找出以下内容时, 实际输出并不完全符合我的预期。

for cl in lines:
    filename="superfamily_new_trail_"+str(cl)
    a=filename.strip()
    f=open(a,'r')
    lines2=f.readlines()
    for line2 in lines2:
        if (not "====="  in line2) and (not"CDD" in line2)and (len(line2)>30):
            Tag=line2.split("\t") 
            print cl+"\t"+Tag[0]+"\t"+Tag[7]+"\t"+Tag[10]

我希望我的输出是

cl    Tag[0]    Tag[7]       Tag[10]

但我的实际打印出来是这样的

cl 
       Tag[0]    Tag[7]     Tag[10]   

在两个不同的行? 它出什么问题了?非常感谢!

【问题讨论】:

  • print repr(Tag) 向您展示了什么?似乎某处有一个额外的回车符。

标签: python printing output


【解决方案1】:

我想你可能在cl 中有一个换行符。你可以这样做:

clStr = str(cl).rstrip()
print clStr+"\t"+Tag[0]+"\t"+Tag[7]+"\t"+Tag[10]

【讨论】:

    【解决方案2】:

    您的cl 字符串末尾有一个额外的换行符。剥离它:

    print cl.rstrip('\n')+"\t"+Tag[0]+"\t"+Tag[7]+"\t"+Tag[10]
    

    这对于从文件中读取的行来说是很正常的; \n 转义序列代表换行符。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-16
      相关资源
      最近更新 更多