2017-10-09 98 views
0

我打印此表中的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 

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個字符,但我所得到的,當我拿到那點是左對齊的第一列。任何想法,爲什麼,我該怎麼辦呢?

+0

[Python的截斷長字符串](https://stackoverflow.com/q/2872512/669576) –

回答

0

插入符^後的數字指定的總寬度,其用於對準。點.後的數字是用來調整字符串,請參閱語言spec

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

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

你也應該看看tabulate