I have some values that I want to write in a text file with the constraint that each value has to go to a particular column of each line.
For example, lets say that I have values = [a, b, c, d] and I want to write them in a line so that a is going to be written in the 10th column of the line, b on the 25th, c on the 34th, and d on the 48th column.
How would I do this in python?
Does python have something like column.insert(10, a)? It would make my life way easier.
I appreciate your hep.
In this case, I’d think you’d just use the padding functions with python’s string formatting syntax.
Something like
"%10d%15d%9d%14d"%valueswill place the right-most digit of a,b,c,d on the columns you listed.If you want to have the left-most digits placed there, then you could use:
"%<15d%<9d%<14d%d"%values, and prepend 10 spaces.EDIT: For some reason I’m having trouble with the above syntax… so I used the newstyle formatting syntax like so:
" "*9 + "{:<14}{:<9}{:<14}{}".format(*values)This should print, for
values=[20,30,403,50]: