I have written a script which parses a UTF-8 text file and prints the required result. The code is below:

import os
import sys
import string
import csv
import codecs
usage = "usage: Specify the directories for files and the RDOCK cut-off value"
if len(sys.argv) < 1:
    print usage
    f=codecs.open(sys.argv[1],  encoding='utf-8')
    for line in f:
        if line[0:3] =="The":
            print line[56:]
        elif line[0:4] == "Stru":
            print line[24:]
        elif line[0:4] == "RMSD":
            print line
        elif line[0:4] =="TM-s":
            print line
        elif line [0:4] == "MaxSu":
            print line
        elif line[0:4] =="GDT-":
            print line
        elif line[0:4] =="Supe":
            print line
            print "-------------------------------------------------------------------------------------"

The output given is:



Length=  917

Length=  917 (by which all scores are normalized)

RMSD of  the common residues=   38.213

TM-score    = 0.3821  (d0=10.18, TM10= 0.3501)

GDT-TS-score= 0.3501 %(d<1)=0.3501 %(d<2)=0.3501 %(d<4)=0.3501 %(d<8)=0.3501

GDT-HA-score= 0.3501 %(d<0.5)=0.3501 %(d<1)=0.3501 %(d<2)=0.3501 %(d<4)=0.3501

Superposition in the TM-score: Length(d<5.0)=321  RMSD=  0.00

Though the output is correct, I want them to be printed on the same line instead of multiple lines? Can anyone help me?

Edited by chavanak: n/a

7 Years
Discussion Span
Last Post by Beat_Slayer

First when you iterate over the lines, there is a newline character at the end of each line. This newline can be removed with line = line.rstrip("\r\n") . You should write this as the first statement in the for loop. Then I suggest that you invoke an output function instead of printing lines. It means that you replace all the print line[4:] by output(line[4:]) . This allows you to change the effect of your code just by defining the output function. For example you can write

import sys
def output(data):

Later, you can change this function definition to print to a file, or add newlines, etc.

Votes + Comments
Helped me a lot and answered my question

hi ,i m unable to make a grid like 0000
0000 .... plz can anybody help me. how to print output
in a same line.
for row in range(3):
for col in range(4):
print (0)
print ()


I assume that you are using Python 3 from the function style prints. You can give end keyword parameter the letter you want inserted after print, if you want print right after previous one for example end="".

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.