【问题标题】:Store each loop, and output to table at loop completion存储每个循环,并在循环完成时输出到表
【发布时间】:2017-01-10 22:18:24
【问题描述】:

我正在尝试将某些 SSL 发现扫描的结果作为表格输出以用于报告目的,但我在以我想要的方式解析该数据时遇到了一些问题。

我希望输出如下所示:

IP Address    Common Name             Valid To
------------  ----------------------  ----------
10.0.255.250   ex.example.com  2017/02/09
10.0.255.251   localhost      2009/07/04
10.0.255.252    ex.example2.com     2016/05/24

它看起来像这样。

IP Address    Common Name             Valid To
------------  ----------------------  ----------
10.0.255.250   ex.example.com  2017/02/09
IP Address    Common Name    Valid To
------------  -------------  ----------
10.0.255.251   localhost      2009/07/04
None
IP Address    Common Name    Valid To
------------  -------------  ----------
10.0.255.252    ex.example2.com     2016/05/2

我一直在尝试使用 while 循环来解决这个问题,但没有任何运气。它似乎不是“while”循环,而更像是“for each”循环。

def tabulateText():
    loop = True
    while loop == True:
        with open("testinput.txt", "r") as text_file:
            for line in text_file:
                if "end" in line:
                    loop = False
                elif "IP Address =" in line:
                    start = line.find('IP Address = ')
                    endline = line.find('\n', start)
                    ip = line[start+13:endline]
                    cert = SSLmon(ip)
                    Col1 = ip
                    FCS2 = cert.find('commonName')
                    FCE2 = cert.find('/', FCS2)
                    FCS2b = cert.find('commonName')
                    FCE2b = cert.find('\n', FCS2b)
                    Colopt2 = cert[FCS2+11:FCE2]
                    Colopt2b = cert[FCS2b+11:FCE2b]
                    Col2 = cert[FCS2+11:FCE2] if len(Colopt2) < len(Colopt2b) else cert[FCS2b+11:FCE2b]
                    FCS3 = cert.find('Not valid after:')
                    FCE3 = cert.find('T', FCS3)
                    Col3 = cert[FCS3+18:FCE3].replace('-', '/')
                    column = Col1[n], Col2[n], Col3[n]
                    print(tabulate([column], headers=['IP Address', 'Common Name', 'Valid To']))
                else:
                    pass

print(tabulateText())
print(tabulateText())
print(tabulateText())

【问题讨论】:

  • 您将分别对每个line in text_file 进行制表。你有一个for(每个)循环在你的while循环中;外循环只运行一次!尝试将tabulate 移出for 循环,并完全摆脱while
  • 另外,请注意: 1. 您永远不会尝试做您在标题中描述的事情;和 2. 发现 endline 是多余的 - \n 是(不可避免地!)行中的最后一个字符。
  • @jonrsharpe End 是我用作输入的文本文件中的最后一个单词。我希望每个循环都存储三列,并在遍历输入文档后,以正确的列顺序将这些值打印到行。

标签: python for-loop while-loop


【解决方案1】:

print(tabulate([column], headers..) 命令在每次读取“IP 地址”行后执行,这就是为什么你看到标题和只有一行数据的原因。

您可以做的是将每一行结果(称为“列”)附加到一个数组中。这将创建一个包含所有结果的表格。 在读取文件的所有行之后执行 print(tabulate()) 命令。

首先,在读取文本文件之前创建一个空数组——我称之为表格:

table = [] 
  with open("testinput.txt", "r") as text_file:

将您的列数组附加到表数组 (table.append[column]) 并消除您当前使用的 print(tabulate()) 命令。

column = Col1[n], Col2[n], Col3[n]
table.append[column]

在阅读完文件并引用新变量“table”之后,将 print(tabulate()) 命令移到最后。

print (tabulate(table), headers=['...'])

你不需要循环——“for line”命令循环遍历文本文件。另外,完成后关闭文件(text_file.closed)

函数看起来像这样:

table = []
with open("testinput.txt", "r") as text_file:
    for line in text_file:
        if "IP Address =" in line:
            start = line.find('IP Address = ')
            endline = line.find('\n', start)
            ip = line[start+13:endline]
            cert = SSLmon(ip)
            Col1 = ip
            FCS2 = cert.find('commonName')
            FCE2 = cert.find('/', FCS2)
            FCS2b = cert.find('commonName')
            FCE2b = cert.find('\n', FCS2b)
            Colopt2 = cert[FCS2+11:FCE2]
            Colopt2b = cert[FCS2b+11:FCE2b]
            Col2 = cert[FCS2+11:FCE2] if len(Colopt2) < len(Colopt2b) else cert[FCS2b+11:FCE2b]
            FCS3 = cert.find('Not valid after:')
            FCE3 = cert.find('T', FCS3)
            Col3 = cert[FCS3+18:FCE3].replace('-', '/')
            column = Col1[n], Col2[n], Col3[n]
            table.append[column]
    text_file.closed
    print(tabulate([table], headers=['IP Address', 'Common Name', 'Valid To']))

如果您执行 print(table) 命令,表格数组本身将如下所示:

[['10.0.255.250','ex.example.com','2017/02/09'],['10.0.255.251','localhost', '2009/07/04'],[' 10.0.255.252','ex.example2.com','2016/05/24']]

【讨论】:

    【解决方案2】:

    我最终采用了不同的方法来获取我想要的数据。我终于像这样打印了每一列以及由.ljust() 控制的填充。

    with open("testinput.txt", "r") as text_file:
        for line in text_file:
            if "IP Address =" in line:
                start = line.find('IP Address = ')
                endline = line.find('\n', start)
                ip = line[start+13:endline]
                cert = SSLmon(ip)
                Col1 = ip
                FCS2 = cert.find('commonName')
                FCE2 = cert.find('/', FCS2)
                FCS2b = cert.find('commonName')
                FCE2b = cert.find('\n', FCS2b)
                Colopt2 = cert[FCS2+11:FCE2]
                Colopt2b = cert[FCS2b+11:FCE2b]
                Col2 = cert[FCS2+11:FCE2] if len(Colopt2) < len(Colopt2b) else cert[FCS2b+11:FCE2b]
                FCS3 = cert.find('Not valid after:')
                FCE3 = cert.find('T', FCS3)
                Col3 = cert[FCS3+18:FCE3].replace('-', '/')
                with open(filename1, 'a') as f:
                    line = '%s %s %s %s' % (Col1.ljust(20), Col2.ljust(35), Col3.ljust(15), '\n')
                    f.write(line)
    

    【讨论】:

      猜你喜欢
      • 2021-04-23
      • 2014-12-13
      • 1970-01-01
      • 2023-02-24
      • 1970-01-01
      • 2014-07-19
      • 1970-01-01
      • 1970-01-01
      • 2016-11-05
      相关资源
      最近更新 更多