如何在python中正确格式化多列整数?

时间:2019-02-03 20:13:36

标签: python

我在这里有一些代码:

for i in range(self.size):
    print('{:6d}'.format(self.data[i], end=' '))
        if (i + 1) % NUMBER_OF_COLUMNS == 0:
            print()

现在,此打印为:

1 1 1 1 1 2 3 3 3 3 (whitespace) 3 3 3 etc. 当它达到10位数字时,它会创建一个新行,但不会连续显示前10个...

这就是我想要的-

1 1 1 1 1 1 1 2 2 3
3 3 3 3 3 4 4 4 4 5

但是当它碰到两位数字时,它就会变得混乱-

8 8 8 8 8 9 9 9 9 10
10 10 10 10 10 10 etc.

我希望它像这样右对齐-

  8  8  8  8  8  9
 10 10 10 10 11 12 etc.

当我删除格式部分时,它将打印出行,但是当然不会有多余的间隔!

2 个答案:

答案 0 :(得分:0)

您可以使用字符串的.rjust方法通过“填充”值来对齐字符串。使用一些虚拟数据:

NUMBER_OF_COLUMNS = 10
for i in range(100):
    print("{}".format(i//2).rjust(3), end=' ')
    #print("{:3}".format(i//2), end=' ') edit: this also works. Thanks AChampion
    if (i + 1) % NUMBER_OF_COLUMNS == 0:
        print()

#Output:
  0   0   1   1   2   2   3   3   4   4 
  5   5   6   6   7   7   8   8   9   9 
 10  10  11  11  12  12  13  13  14  14 
 15  15  16  16  17  17  18  18  19  19 
 20  20  21  21  22  22  23  23  24  24 
 25  25  26  26  27  27  28  28  29  29 
 30  30  31  31  32  32  33  33  34  34 
 35  35  36  36  37  37  38  38  39  39 
 40  40  41  41  42  42  43  43  44  44 
 45  45  46  46  47  47  48  48  49  49 

答案 1 :(得分:0)

另一种方法是将数据分块成行并打印每一行,例如:

{
"Response":{
  "MetaInfo":{
     "Timestamp":"2019-02-03T20:41:00.395+0000"
  },
  "View":[
     {
        "_type":"SearchResultsViewType",
        "ViewId":0,
        "Result":[
           {
              "Relevance":1.0,
              "MatchLevel":"postalCode",
              "MatchQuality":{
                 "PostalCode":1.0
              },
              "Location":{
                 "LocationId":"NT_CwZliV687TLYW4ZZKm4VNA",
                 "LocationType":"point",
                 "DisplayPosition":{
                    "Latitude":50.8082,
                    "Longitude":-0.39127
                 },
                 "NavigationPosition":[
                    {
                       "Latitude":50.8082,
                       "Longitude":-0.39127
                    }
                 ],
                 "MapView":{
                    "TopLeft":{
                       "Latitude":50.82169,
                       "Longitude":-0.41262
                    },
                    "BottomRight":{
                       "Latitude":50.79471,
                       "Longitude":-0.36992
                    }
                 },
                 "Address":{
                    "Label":"BN11 3PQ, Worthing, England, United Kingdom",
                    "Country":"GBR",
                    "State":"England",
                    "County":"West Sussex",
                    "City":"Worthing",
                    "PostalCode":"BN11 3PQ",
                    "AdditionalData":[
                       {
                          "value":"United Kingdom",
                          "key":"CountryName"
                       },
                       {
                          "value":"England",
                          "key":"StateName"
                       },
                       {
                          "value":"West Sussex",
                          "key":"CountyName"
                       }
                    ]
                 }
              }
           }
        ]
     }
  ]
}
}

例如:

def chunk(iterable, n):
    return zip(*[iter(iterable)]*n)

for row in chunk(self.data, NUMBER_OF_COLUMNS):
    print(' '.join(str(data).rjust(6) for data in row))