简体   繁体   English

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

[英]Formatting the output by proper spacing in python

I am trying to format the output that i am receiving and the method that I am trying is not helping much 我正在尝试格式化我收到的输出,我正在尝试的方法没有多大帮助

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

the output seems to be not in line, is there a way to format it more presentably ? 输出似乎不符合要求,有没有办法更方便地格式化它?

the output i am getting 我得到的输出

`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>

Expected output : 预期产量:

`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>

Thanks! 谢谢!

Your problem is that your values are longer then your given format strings: 你的问题是,你的价值观是不再那么您给定的格式字符串:

*155sfjfnfjv7qeeefefefe*
123456789012345678901234   # 24 characters long

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

You can make your printing adapt to the longest value of each key. 您可以使打印适应每个键的最长值。 You have no missing keys - this could look like so: 你没有丢失的钥匙 - 这可能是这样的:

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

Output: 输出:

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 your expected output, it should be 对于您的预期输出,它应该是

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

It should be < not >

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM