Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Streamline this format code
#1
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
Reply
#2
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().
Reply
#3
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 they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
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
Reply
#5
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]: 
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
No, hdr62 has seven arguments, I just did not list them all -- for brevity
Reply
#7
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.
Reply
#8
(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
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#9
Thanks - I will read up on how to "unpack"
Reply
#10
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  An unexplainable error in .format statement - but only in a larger piece of code? ToniE 4 656 Sep-05-2023, 12:50 PM
Last Post: ToniE
  Please suggest python code to format DNA sequence FASTA file rajamdade 4 3,117 Oct-24-2019, 04:36 AM
Last Post: rajamdade
  code help to convert log file to csv format bmunagala 3 12,088 Jan-21-2019, 06:08 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020