如何设置文本行的自动宽度?

2024-09-27 04:23:13 发布

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

我有python函数:

def myFunction(inputList):
    outputList = ['Path      |      Type      |      Size']
    for item in inputList:
        outputList.append(item + '      |      ' + parseFileType(item) + '      |      ' + parseFileSize(item))
    return outputList

这个函数有如下输出:

Path      |      Type      |      Size
/etc/acpi       |      directory      |      16 Kb.
/etc/adduser.conf       |      ASCII text      |      4,0 Kb.
/bin/df |      ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=8b0d9cd8d2256a94b9ea7e2248b6d8721cff588d, stripped      |      84 Kb.

如何对此函数的文本输出设置自动宽度,因为它看起来像:

Path                |      Type           |      Size
/etc/acpi           |      directory      |      16 Kb.
/etc/adduser.conf   |      ASCII text     |      4,0 Kb.
...

Tags: path函数forsizekbconftypeascii
1条回答
网友
1楼 · 发布于 2024-09-27 04:23:13

In Python, strings have a built-in method named ljust. The method lets you pad a string with characters up to a certain length.

The ljust method means "left-justify"; this makes sense because your string will be to the left after adjustment up to the specified length.

您可以尝试这样做,以确保每个字符串都具有相同的长度,这将保证您的列看起来像您希望的那样

相关问题 更多 >

    热门问题