Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ascii python格式表_Python - Fatal编程技术网

ascii python格式表

ascii python格式表,python,Python,您好,我正在尝试在第二列之后添加一个空格,但没有将其添加到第一列,但是如果我尝试以肮脏的方式添加它,如“|”。join我的单元格变得一团糟,但是我如何才能获得所需的输出?我还尝试了rjust(3),但它不起作用 _id = ['1', '2', '3','4'] transport = ['http','tcp','https','dns'] agent = ['10.10.1.1','10.10.1.2','10.10.1.3','10.10.1.4'] username = ['DESKTO

您好,我正在尝试在第二列之后添加一个空格,但没有将其添加到第一列,但是如果我尝试以肮脏的方式添加它,如
“|”。join
我的单元格变得一团糟,但是我如何才能获得所需的输出?我还尝试了
rjust(3)
,但它不起作用

_id = ['1', '2', '3','4']
transport = ['http','tcp','https','dns']
agent = ['10.10.1.1','10.10.1.2','10.10.1.3','10.10.1.4']
username = ['DESKTOP-123\\user','root','user','user']
os = ['windows/amd64','linux/amd64','windows/amd64','linux/amd64']
seen = ['2019-08-31 13:10:08','2019-08-31 13:10:08','2019-08-31 13:10:08','2019-08-31 13:10:08']
titles = ['ID', 'Transport', 'Agent', 'Username','Operating System','Last Seen']
data = [titles] + list(zip(_id, transport, agent, username,os,seen))

def os():
    print("\t")
    for i, d in enumerate(data):
        line = '|'.join(str(x).ljust(18) for x in d)
        print(line)
        if i == 0:
            sep = '-' * 18 + '+'
            line = ''.join(sep for x in d)
            print(line)
    print("\t")
我想让一个输出在管线执行第一列后添加一个空格

|1 | 2 | 3 |
+--+---+---+
|4 | 5 | 6 |
|7 | 8 | 9 |

将枚举数据的第一行定义替换为:

line = f'{str(d[0]).ljust(18)}| {"| ".join(str(x).ljust(17) for x in d if d.index(x) > 0)}'
可能是这样的:

_id = ['1', '2', '3','4']
transport = ['http','tcp','https','dns']
agent = ['10.10.1.1','10.10.1.2','10.10.1.3','10.10.1.4']
username = ['DESKTOP-123\\user','root','user','user']
os = ['windows/amd64','linux/amd64','windows/amd64','linux/amd64']
seen = ['2019-08-31 13:10:08','2019-08-31 13:10:08','2019-08-31 13:10:08','2019-08-31 13:10:08']
titles = ['ID', 'Transport', 'Agent', 'Username','Operating System','Last Seen']
data = [titles] + list(zip(_id, transport, agent, username,os,seen))

def os():
    print("\t")
    for i, d in enumerate(data):
        line = f'{str(d[0]).ljust(18)}| {"| ".join(str(x).ljust(17) for x in d if d.index(x) > 0)}'
        print(line)
        if i == 0:
            sep = '-' * 18 + '+'
            line = ''.join(sep for x in d)
            print(line)
    print("\t")
结果:

ID                | Transport        | Agent            | Username         | Operating System | Last Seen        
------------------+------------------+------------------+------------------+------------------+------------------+
1                 | http             | 10.10.1.1        | DESKTOP-123\user | windows/amd64    | 2019-08-31 13:10:08
2                 | tcp              | 10.10.1.2        | root             | linux/amd64      | 2019-08-31 13:10:08
3                 | https            | 10.10.1.3        | user             | windows/amd64    | 2019-08-31 13:10:08
4                 | dns              | 10.10.1.4        | user             | linux/amd64      | 2019-08-31 13:10:08

看起来表格软件包()会让你的生活更轻松。是的,但我是这样做的,我几乎要完成了,但是这个空间让人很痛苦