Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Formatting $
#10
I am new to python and am having trouble formatting a variable.  I read several variables from a file, print them in columns, and would like certain columns to come out like this:

             $12
         $1,234
 $12,000,000

Right justified, $ next to first number, commas inserted, no decimals (not lopped off, just rounded to nearest $)

here is the couple of lines of code that reads the file (all numbers), prints headers, and prints the variables.  Notice that I have tried two different ways to format (again, I am just learning, so I am just trying stuff).  The read part works -- I get output -- it is just not formatted.  I will post entire code if someone wants.
print("Year", "\t", "Your", "\t", "Spouse", "\t", "Taxable", "\t", "Taxable", "\t", "Taxable")
print("\t", "\t", "Age", "\t", "Age", "\t", "Equity", "\t", "Fixed Income", "\t", "Cash")

with open("test.txt", "r") as opened_file:
    for line in opened_file:
        start_year, your_start_age, her_start_age, taxableequity, taxablefixedincome, taxablecash, taxableequitygwth, \
        taxablefixedincomediv, taxablecashdiv = line.split()
        print(start_year, "\t", your_start_age, "\t", her_start_age, "\t", '{}'. format('$' + str(taxableequity)), "\t",
              '{:{}{}.{}}'.format(taxablefixedincome, '>', 10, 10), "\t", taxablecash)
What do I change in either method to achieve my desired results?  Thanks
Reply


Messages In This Thread
Formatting $ - by birdieman - Dec-22-2016, 03:07 PM
RE: Formatting $ - by snippsat - Dec-22-2016, 03:26 PM
RE: Formatting $ - by birdieman - Dec-22-2016, 03:32 PM
RE: Formatting $ - by snippsat - Dec-22-2016, 03:46 PM
RE: Formatting $ - by birdieman - Dec-22-2016, 04:00 PM
RE: Formatting $ - by wavic - Dec-22-2016, 04:43 PM
RE: Formatting $ - by birdieman - Dec-22-2016, 05:08 PM
RE: Formatting $ - by wavic - Dec-22-2016, 05:21 PM
RE: Formatting $ - by snippsat - Dec-22-2016, 06:22 PM
RE: Formatting $ - by wavic - Dec-25-2016, 06:25 AM
RE: Formatting $ - by birdieman - Dec-25-2016, 01:01 PM
RE: Formatting $ - by wavic - Dec-25-2016, 01:15 PM
RE: Formatting $ - by birdieman - Dec-25-2016, 03:55 PM
RE: Formatting $ - by wavic - Dec-25-2016, 04:37 PM
RE: Formatting $ - by birdieman - Dec-25-2016, 05:43 PM
RE: Formatting $ - by wavic - Dec-25-2016, 06:37 PM
RE: Formatting $ - by birdieman - Dec-25-2016, 08:51 PM
RE: Formatting $ - by wavic - Dec-25-2016, 10:14 PM
Formatting $ - by birdieman - Dec-24-2016, 05:25 PM

Forum Jump:

User Panel Messages

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