Tuesday, 6 August 2013

Python: Change number of columns in formatting

Python: Change number of columns in formatting

If I have 3 different values say foo1, foo2, and foo3 and I want an output
that looks something like this:
----- Some Header ------
foo1
foo2
foo3
And then for whatever reason I get rid of one of the values, say foo2,
then I want the output to look like this:
----- Some Header ------
foo1
foo3
Now, I think I've figured out how to do most of this using .format for
strings and a list or dictionary as the parameter. What I'm not sure how
to do is still have only one line of formatting adding to the container
that I'm using changing how much space it uses for the paragraphs.
So, my need, I'd like to have just one line of code formulate the
different lines when I have 3 foos and when I have 2 foos. Something like:
foos = ['foo1', 'foo2', 'foo3']
printFoos = []
if (len(foos) == 3):
incr = 10
elif (len(foos) == 2):
incr = 15
space = 0
for x in foos:
space += incr
printFoos.append('{:space}\n'.format(x))
And then eventually it prints out all of the lines in printFoos making the
first output wanted. And when it comes through again with len(foos) = 2
then it'll make the second output wanted.

No comments:

Post a Comment