如何正确格式化 python 中的控制台输出?

问题描述 投票:0回答:1
from colorama import Fore
import platform
import distro
import time
import os

# Functions
def get_uptime():
    return os.popen('uptime -p').read()[:-1]

def get_os():
    return platform.system() + " " + platform.release()

# Main Entry
def main():
    linux_distro = distro.name()
    os_string = f"OS: {get_os()}"
    uptime_str = f"Uptime: {get_uptime()}"
    if 'pop' in linux_distro.lower():
        print(Fore.CYAN + f"""


            ←←←←←←←←←←←              ╠═══════════════ ROGUE FETCH ═════════════════════════════╣
        ←←←←←←←←←←←←←←←←←←←          ╠  {os_string}                                            ╣
      ←←←←←←    ←←←←←←←←←←←←←        ╠                                                         ╣
    ←←←←←         ↖←←←←←←←←←←←←      ╠  {uptime_str}                                           ╣
   ←←←←     ←←      ←←←←←←←←←←←←     ╠                                                         ╣
 ↖←←←←←     ←←←     ←←←←←←←←←←←←←↖   ╠                                                         ╣
 ←←←←←←←     ←←←    ←←←    ↖←←←←←←   ╠                                                         ╣
←←←←←←←←←     ←↖   ↖←←↖    ←←←←←←←←  ╠═════════════════════════════════════════════════════════╣
←←←←←←←←←←        ←←←←    ↖←←←←←←←←
←←←←←←←←←←←    ↖←←←←←←   ↖←←←←←←←←←
←←←←←←←←←←←←    ←←←←←←  ←←←←←←←←←←←
←←←←←←←←←←←←←    ←←←←←↖←←←←←←←←←←←←
 ←←←←←←←←←←←←←   ↖←←←↖ ←←←←←←←←←←← 
 ↖←←←←←←←←←←←←←  ←←←←  ←←←←←←←←←←↖ 
   ←←←←←←←←←←←←←←←←←←←←←←←←←←←←←   
    ←←←←                   ←←←←    
      ←←←←←←←←←←←←←←←←←←←←←←←      
        ←←←←←←←←←←←←←←←←←←←        
            ←←←←←←←←←←←            
  
        """)

我用符号制作了框架,但是在运行时,它看起来没有对齐并且看起来错误: 链接:https://media.discordapp.net/attachments/1164305090144915556/1170754515495030895/image.png?ex=655a311c&is=6547bc1c&hm=41877fdc735ae2b06a1c7588f7c7ba397c2dd 69cf8229d2ef6df6348379263f9&=&宽度=1160&高度=487

python terminal console alignment ascii
1个回答
0
投票

您已根据“{uptime_str}”长度而不是所包含字符串的实际长度对齐文本。

使用

len(uptime_str)
然后使用一些填充机制,例如:

"mystring".rjust(how_much_to_pad)

"mystring".ljust(how_much_to_pad)

" " * len(uptime_str)

© www.soinside.com 2019 - 2024. All rights reserved.