Search code examples
pythonfixed-width

Stuff spaces at end of lines in file


I am trying to read a fixed with file that has lines/records of different lengths. I need to stuff spaces at the end of the lines which are less than the standard length specified.

Any help appreciated.

enter image description here


Solution

  • You could consider to use ljust string method. If line is a line read from your file:

    line = line.ljust(50)
    

    will stuff the end of the line with spaces to get a 50 characters long line. If line is longer that 50, line is simply copied without any change.