简体   繁体   中英

python print out is not in the same line

I have question, when I try to pint out following things in python in the sameline, the actually output is not exactly as I expected.

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]

I was hoping my output would be

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

but my actually print out is like

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

in two different lines? what's wrong with it? Thanks a lot!

I think you might have a newline in cl . You can do this:

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

Your cl string has an extra newline at the end. Strip it:

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

This is quite normal for lines read from a file; the \\n escape sequence stands for a newline.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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