Python to merge rows

Transpose using python

@jagking I was looking into this code and was trying to refine it further,
1.) if the length of line is less than 50 , (suppose the length of line is 45) it should insert 5 blank spaces to streamline the data, i was trying it with all possible cases but it is not appending the blank spaces to the last line when only single line is there for ex:

2.) in case xyz occurs, it should skp the line.

I was expecting that it should append blank spaces to make each row of length 50:
for example:
abc 123 456 #if row is less than 50 , it should append blank spaces to sync with all other rows

def format_file(inFile, outFile,dic):
    expected = 0
    gap = " "*50
    newLine = []
    join = " ".join
    write = outFile.write

    for line in inFile:
        sLine = line[:-1]
        linePos = dic[line[:3]]
        if linePos == 4:
        if linePos == 0:  
            if newLine:
                if len(line)!=len1:
                newline.append(" "*gap2)
                newline = [sLine]
            newLine = [sLine]
            expected = 1
            gaps = linePos - expected
            if gaps != 0:
                expected = linePos + 1
                expected += 1
            if len(line)!=len1:
            newline.append(" "*gap2)

    if newLine:
        newLine[-1] = line
        if len(line)!=len1:
        newline.append(" "*gap2)

def main():
    positions = {"abc":0,
    with open("Text.txt","r") as inTxt, open("NewText.txt","w")as outTxt:
if __name__ == "__main__":

If you want to have the line as a minimum of 50 characters long, use string formatting. E.g. “{: <50}”.format(sLine). It goes :[character to use to fill in the deficit][direction to push the text][minimum length of the string]. So we use [space][left arrow so spaces are added to the end of the string][50 characters minimum].

Don’t have continue for xyz. Instead put “\n” or chr(10) to denote a new line. Continue just moves onto the next item on the for loop without doing anything else after the continue statement.
EDIT: Miss read it, I think you mean if xyz occurs you want it to discard the whole line. Right? You will need to reset newLine to an empty list before continue if that is the case…

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.