在python中通过适当的间距格式化输出

时间:2019-05-24 05:28:44

标签: python formatting

我正在尝试格式化我正在接收的输出,而我正在尝试的方法没有太大帮助

new_line = '{:>12}  {:>12}                    {:>8}  {:>12}'.format(s, k, g, j)
print(new_line)

输出似乎不符合要求,是否有办法更合理地格式化它?

我得到的输出

`Change Id`: *155sfjfnfjv7qeeefefefe*   `Service`: *test2121_dkfdb*                     `TRS Environment`: *prod*   `Audit`: <localhost:9090>
`Change Id`: *15sldjnfs56766qedeq*   `Service`: *testkbhhb_236r526ffdf*                     `TRS Environment`: *prod*   `Audit`: <localhost:9090:123>
`Change Id`: *155aoufheufh788478*   `Service`: *testfdjhg*                     `TRS Environment`: *prod*   `Audit`: <locaefbhlkf:23526/kdjfg/kfhgb/lkrfbjke/kj>

预期输出:

`Change Id`: *155sfjfnfjv7qeeefefefe*     `Service`: *test2121_dkfdb*               `TRS Environment`: *prod*     `Audit`: <localhost:9090>
`Change Id`: *15sldjnfs56766qedeq*        `Service`: *testkbhhb_236r526*            `TRS Environment`: *prod*     `Audit`: <localhost:9090:123>

谢谢!

2 个答案:

答案 0 :(得分:1)

您的问题是您的值要比您指定的格式字符串长

*155sfjfnfjv7qeeefefefe*
123456789012345678901234   # 24 characters long

{:>12}  {:>12}                    {:>8}  {:>12}  # 1st data point is 12 chars aligned, but
                                                 # too long so remaining text gets shoved

您可以使打印适应每个键的最长值。您没有丢失的键-看起来像这样:

data = [{"Change Id": "155sfjfnfjv7qeeefefefe", "Service": "*test2121_dkfdb*",
         "TRS Environment": "*prod*", "Audit": "<localhost:9090>"},
        {"Change Id": "*15sldjnfs56766qedeq*", "Service":"*testkbhhb_236r526ffdf*",
         "TRS Environment":"*prod*","Audit":"<localhost:9090:123>"},
        {"Change Id":"*155aoufheufh788478*","Service":"*testfdjhg*",
         "TRS Environment":"*prod*","Audit":"<locaefbhlkf:23526/kdjfg/kfhgb/lkrfbjke/kj>"} ]


# for each dict of your data, for each key, get the length and store 
# the maximum length over all dicts values for that key
lengths = {}
for d in data:
    for key,value in d.items():
        lengths.setdefault(key,0)  # store the lenght of the value under this keys name
        lengths[key] = max(lengths[key],len(value)) # keep the max length for this key

# order of data printout by keyname
order = [ "Change Id","Service","TRS Environment","Audit"]

# for each dict in data print (in the order of keys wanted)
for d in data:
    for o in order:
        # one key spaced by its maximum length+4 over all dicts
        print(f"{o}: {d[o]:{lengths.get(o) + 4}}", end="")
    print() # add newline

输出:

Change Id: 155sfjfnfjv7qeeefefefe    Service: *test2121_dkfdb*           TRS Environment: *prod*    Audit: <localhost:9090>                                
Change Id: *15sldjnfs56766qedeq*     Service: *testkbhhb_236r526ffdf*    TRS Environment: *prod*    Audit: <localhost:9090:123>                            
Change Id: *155aoufheufh788478*      Service: *testfdjhg*                TRS Environment: *prod*    Audit: <locaefbhlkf:23526/kdjfg/kfhgb/lkrfbjke/kj>    

答案 1 :(得分:-1)

对于您的预期输出,应该是

new_line = '{:<12}{:<12}{:<8}{:<12}'.format(s, k, g, j)
print(new_line)

It should be < not >