如何在换行符上继续str.ljust()?

2024-09-19 23:30:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用L4打印了很多值,如下所示:

for cog in bot.cogs:
    cogobj = bot.get_cog(cog)

    try:
        print('+' + '-' * 105 + '+')
        print('| ' + cogobj.qualified_name.upper())
        if cogobj.description:
            print('| ' + cogobj.description)
        print('+' + '-' * 105 + '+')
    except AttributeError:
        pass

    for c in cogobj.get_commands():
        print(f"• {c.name} {c.signature.replace('_', '')}".ljust(35, ' ') + f' {c.help}')
        if isinstance(c, commands.Group):
            for sc in c.commands:
                print(''.ljust(5, ' ') + f"▪ {sc.name} {sc.signature.replace('_', '')}".ljust(35, ' ') + f' {sc.help}')
    print('\n')

这会打印出这样的内容

+---------------------------------------------------------------------------------------------------------+
| FUN
| Category with fun commands
+---------------------------------------------------------------------------------------------------------+
• embed <title> <description>       Will send an embed with <title> and <description>, who's colour will change every 10 seconds
• channel                           Base command for creating or removing a lot of channels in a guild
     ▪ create <amount> <type> [name]     Will create <amount> of text or voice channels in guild
     ▪ remove <amount>                   Will remove <amount> of text and voice channels in a guild

但问题是,有时当c.helpcs.help长时,或者当我使用\n字符时,它会跳入一个新行,使其看起来像这样:

• eval <body>                       Will evaluate python code and discord.py code.
This means you can basically run any script anywhere anytime.
...

当我希望它看起来像

• eval <body>                       Will evaluate python code and discord.py code.
                                    This means you can basically run any script anywhere anytime.

我用tablate尝试过这个,但这会创建一个奇怪的矩阵表

所以我想我的问题是,有没有一种方法可以设置一个字符串的最大长度,它可以自动加入一个具有相同ljust的新行


Tags: andnameinforhelpcodedescriptionamount