How to print information in table format in python?

0

I would like to print the results in the table format as attached image.

In the code I'm doing it is to literally reproduce the above funding system. But when I go to test, they print one below the other and I do not know how to print next to each other as in the image.

    
asked by anonymous 20.06.2018 / 02:10

1 answer

2

Just use the format method of string :

>>> '{:>13,.2f}'.format(100000)
'   100,000.00'

Where:

  • The keys, {} , define a group referring to the parameters of format ;
  • The colon, : , starts the rules for formatting the value;
  • The plus sign, > , defines that the right alignment;
  • The number 13,% with% of itself, defines the total space of the output;
  • The comma, 13 , represents the separator character of thousands;
  • The dot, , , starts the formatting rules for the decimal part;
  • Number 2,% with% itself, defines that there will be only 2 decimal places;
  • The letter f, . , defines that the input will be a float type;

Then read 2 as: format this float number with 2 decimal places, using the comma as the thousands separator, right-aligned in a space of 13 characters. p>

Assuming you have each row in a list, you could do:

>>> linha = [1, 100000.00, 9282.21, 8333.33, 948.88, 91666.67]
>>> print('{:^6}   {:>13,.2f}   {:>9,.2f}   {:>11,.2f}   {:>9,.2f}   {:>11,.2f}'.format(*linha))
'  1         100,000.00    9,282.21      8,333.33      948.88     91,666.67'

Following the reasoning, with few complements to the above code, you easily produce output:

--------------------------------------------------------------------------
Número   Saldo Inicial   Prestação   Amortização     Juros     Saldo Final
------   -------------   ---------   -----------   ---------   -----------
  1         100,000.00    9,282.21      8,333.33      948.88     91,666.67
  1         100,000.00    9,282.21      8,333.33      948.88     91,666.67
  1         100,000.00    9,282.21      8,333.33      948.88     91,666.67
  1         100,000.00    9,282.21      8,333.33      948.88     91,666.67
  1         100,000.00    9,282.21      8,333.33      948.88     91,666.67
--------------------------------------------------------------------------

In versions 3.6+ of Python there are the f-strings calls that facilitate this data formatting. The rules are the same, but instead of using the f method, you enter the name of the variables in the formatting itself, thus leaving the code more readable. The f-string name is due to the prefix {:>13,.2f} that must exist next to the string :

>>> linha = [1, 100000.00, 9282.21, 8333.33, 948.88, 91666.67]
>>> numero, saldo_inicial, prestacao, amortizacao, juros, saldo_final = linha
>>> print(f'{numero:^6}   {saldo_inicial:>13,.2f}   {prestacao:>9,.2f}   {amortizacao:>11,.2f}   {juros:>9,.2f}   {saldo_final:>11,.2f}') 
'  1         100,000.00    9,282.21      8,333.33      948.88     91,666.67'

And, finally, since they are monetary data, do not discard the option to format via format :

>>> import locale
>>> locale.setlocale(locale.LC_ALL, 'pt_BR')
>>> locale.format('%.2f', 100000.00, grouping=True)
'100.000,00'

See also f .

Further readings:

20.06.2018 / 13:54