Python Forum

Full Version: Streamline this format code
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Assuming each variable is to be formatted the same way, is there a way to avoid coding '${:,.0f}' in front of every variable that is to be printed on a line?

print(hdr62.format(
                           '${:,.0f}'.format(ETE).rjust(12, ' '),
                           '${:,.0f}'.format(ETFI).rjust(12, ' '),
                           '${:,.0f}'.format(ETC).rjust(12, ' '),
                           '${:,.0f}'.format(ETTA).rjust(12, ' '),
                           '${:,.0f}'.format(ETFE).rjust(12, ' '),
                           '${:,.0f}'.format(ETFFI).rjust(12, ' '),
                           '${:,.0f}'.format(ETFC).rjust(12, ' ')))
by the way, hdr62 is just a spacing format, like '{:>13}'

thanks
The code
('${:,.0f}'.format(ETE).rjust(12, ' '),
                           '${:,.0f}'.format(ETFI).rjust(12, ' '),
                           '${:,.0f}'.format(ETC).rjust(12, ' '),
                           '${:,.0f}'.format(ETTA).rjust(12, ' '),
                           '${:,.0f}'.format(ETFE).rjust(12, ' '),
                           '${:,.0f}'.format(ETFFI).rjust(12, ' '),
                           '${:,.0f}'.format(ETFC).rjust(12, ' '))
can be compressed to
tuple('${:,.0f}'.format(num).rjust(12, ' ') for num in nums)
assuming that all those numbers are in an ordered collection called nums. You can use unpacking to pass the tuple generated with the above comprehension to hdr62.format().
This is not going to work. The {} represent the position in the formatted string for every argument in format().
'{}, {}, {}, {}, {}'.format('one', 'two', three', 'four', 'five')
As a beginner, I tried this example:
w = 45667.778
x = 56785.55
y = 34529.4568
z = 789612.554

nums = (w, x, y, z)

print(tuple('${:,.0f}'.format(num).rjust(12, ' ') for num in nums))
and got:

('     $45,668', '     $56,786', '     $34,529', '    $789,613')
1. Is that the right way to do it?
2. How do I get rid of the ( ) ' ' and the , between the numbers?

thanks for responding
I mean your first post. You have seven variables but not seven '{}' to print them. 
This part:

print(hdr62.format(
                           '${:,.0f}'.format(ETE).rjust(12, ' '),
                           '${:,.0f}'.format(ETFI).rjust(12, ' '),
                           '${:,.0f}'.format(ETC).rjust(12, ' '),
                           '${:,.0f}'.format(ETTA).rjust(12, ' '),
                           '${:,.0f}'.format(ETFE).rjust(12, ' '),
                           '${:,.0f}'.format(ETFFI).rjust(12, ' '),
                           '${:,.0f}'.format(ETFC).rjust(12, ' ')))
hdr62 holds one '{}' but the format has seven arguments.

In [1]: print('{:>13}'.format(
   ...:                            '${:,.0f}'.format(1).rjust(12, ' '),
   ...:                            '${:,.0f}'.format(2).rjust(12, ' '),
   ...:                            '${:,.0f}'.format(3).rjust(12, ' '),
   ...:                            '${:,.0f}'.format(4).rjust(12, ' '),
   ...:                            '${:,.0f}'.format(5).rjust(12, ' '),
   ...:                            '${:,.0f}'.format(6).rjust(12, ' '),
   ...:                            '${:,.0f}'.format(7).rjust(12, ' ')))
           $1

In [2]: 
No, hdr62 has seven arguments, I just did not list them all -- for brevity
wavic, do you know the contents of the hdr62.format() code? I feel like you're speaking to a different situation.

(Jan-18-2017, 07:40 PM)birdieman Wrote: [ -> ]1. Is that the right way to do it?
It's the right way to simplify that subset of your original code. As I said, to integrate it fully you'll need argument unpacking.

(Jan-18-2017, 07:40 PM)birdieman Wrote: [ -> ]2. How do I get rid of the ( ) ' ' and the , between the numbers?
That'll happen automatically as part of the unpacking.
(Jan-18-2017, 07:48 PM)birdieman Wrote: [ -> ]No, hdr62 has seven arguments, I just did not list them all -- for brevity

Its clear now
Thanks - I will read up on how to "unpack"
I have looked and read about unpacking tuples, and found several examples, but none of the examples refers to printing. I am too much of a beginner to translate those examples into what I need, which micseydel alluded to above: Unpack the tuple, and print all variables using the format hdr62 -- all in one line of code. Here is what I have:
w = 45667.778
x = 56785.55
y = 34529.4568
z = 789612.554

nums = (w, x, y, z)
hdr62 = '{:>15} {:>15} {:>15} {:>15}'
print(tuple('${:,.0f}'.format(num).rjust(12, ' ') for num in nums))
Can someone give me further help? Thanks
Pages: 1 2