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.
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.
You can use string.format to pad a string to a specific length.
The documentation says that <
pads to the right so to pad a string with spaces to the right to a specific length you can do something like this:
>>> "{:<30}".format("foo")
'foo '
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.