user1830108
user1830108

Reputation: 193

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!

Upvotes: 1

Views: 186

Answers (2)

Martijn Pieters
Martijn Pieters

Reputation: 1121864

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.

Upvotes: 0

jh314
jh314

Reputation: 27792

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]

Upvotes: 1

Related Questions