开发者

Spacified columnized on-the-left line formatting to columnized data?

开发者 https://www.devze.com 2023-02-07 19:19 出处:网络
xxxxxxxxxxxxx1.11xxxxxxxxxx1.11xxxxxxxxxxx1.11 xxxxxxxxxxxxx1.11xxxxxxxxxx1.11xxxxxxxxxx11.11 Is there some ready module in Python with which I could easily do above like formatting 17signs, 14 sig
xxxxxxxxxxxxx1.11xxxxxxxxxx1.11xxxxxxxxxxx1.11
xxxxxxxxxxxxx1.11xxxxxxxxxx1.11xxxxxxxxxx11.11
  1. Is there some ready module in Python with which I could easily do above like formatting 17signs, 14 signs and then 15 signs?
  2. How can you get the thing n.b. the small differe开发者_开发技巧nce with 1.11 and 11.11?

N.b. the numbers could be larger but not greater than 10 signs. The linewise count 46 (=17+14+15) is constant.

[Update] Notice that I am using Python 2.6.5 so getting error ValueError: zero length field name in format with suggestions.


You don't need a module or library to do this, use string formatting. To avoid the ValueError, specify what index your parameter is in the supplied values:

"{0:>17}".format(11.1)


String formatting is built-in:

  • http://docs.python.org/library/string.html#format-specification-mini-language

Examples for Python 2.7/3.1

>>> '{:x>17}'.format(s)
'xxxxxxxxxxxx11.11'

>>> '{:x>17}{:x>14}{:x>15}'.format(11.1, 11.1, 11.1)
'xxxxxxxxxxxxx11.1xxxxxxxxxx11.1xxxxxxxxxxx11.1'


Someone commented the right answer but removed it so I will post it here. I will remove it if s/he post it so the credits go to the right person.

('%17.2f%14.2f%15.2f' % (1.11, 1.11, 11.11))
0

精彩评论

暂无评论...
验证码 换一张
取 消