中继长字符串格式 - Python

Trunk long strings in format - Python

我正在 Python 中打印此 table:

          NOMBRE        POBLACIÓN ENFERMOS  SANOS  RATA
       Chile        17000000     0    17000000 0.00 
     Hanslandia      2000000  2000000     0    1.00 
     Bastiland       4000000     0     4000000 -0.01
        Peru        31000000     0    31000000 0.00 
      Bolivia       10000000 10000000     0    1.00 
     Argentina      43000000     0    43000000 0.00 
    Henryboysnia     4200000  4200000     0    1.00 
     Inglaterra     51000000     0    51000000 0.00 
Dondeeldiabloperdioelponcho 500000      0     500000  0.00 
   Laquebradelaji   11000000 11000000     0    1.00 
      Uruguay        2000000     0     2000000 0.00 
      Paraguay       4000000     0     4000000 0.00 
      Suriname       500000      0     500000  0.00 
     Venezuela      31000000 31000000     0    1.00 
   Fantasilandia     3000000     0     3000000 -0.01
        USA         300000000    0    3000000000.00 
       NotUSA       20000000     0    20000000 -0.01
       Mexico       127000000    0    1270000000.00 
     Happyland       5000000     0     5000000 -0.01
UvuvwevweOnyetenyevweUgwembubwemOssas13000000     0    13000000 -0.01

使用此格式行代码:

print("{0: ^20}{1: ^9.0f}{2: ^9.0f}{3: ^9.0f}{4: ^5.2f}".format(mundo[country].name,mundo[country].inhabitants,mundo[country].sick[i],mundo[country].healthy[i],mundo[country].rata[i]))

问题是,这个字符串 vuvwevweOnyetenyevweUgwembubwemOssas 太长了,导致其他列看起来乱七八糟。我已经研究过,据推测,如果我这样做 {0:. ^20}. 会将字符串缩减为 20 个字符,但是当我得到那个点时得到的是第一列左对齐。知道为什么,我该怎么做?

插入符号[=​​11=]后的数字指定总宽度,用于对齐。点.后的数字用于trim字符串,见语言spec.

>>> "{0:^20.20}".format("This-is-much-to-long-to-fit")
'This-is-much-to-long'

>>> "{0:^20.20}".format("sort")
'        sort        '

您还应该看看 tabulate