Python格式的Trunk长字符串

2024-05-15 18:02:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我用Python打印这个表:

          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

使用此格式行代码:

^{pr2}$

问题是,有一个字符串vuvwevweOnyetenyevweUgwembubwemOssas太长,使其他列看起来混乱不堪。我已经研究过了,假设我做了{0:. ^20}.会把这个字符串拖到20个字符,但是当我得到那个点时,我得到的是第一列与左边对齐。你知道为什么吗?我怎么做?在


Tags: 字符串peruchileargentinanombreboliviabastilandsanos
1条回答
网友
1楼 · 发布于 2024-05-15 18:02:07

插入符号^后的数字指定用于对齐的总宽度。点.后面的数字用于修剪字符串,请参见spec语言。在

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

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

您还应该看看tabulate。在

相关问题 更多 >