为了账号安全,请及时绑定邮箱和手机立即绑定

使用标题将文本解析为 python 中的 csv-F5 LTM

使用标题将文本解析为 python 中的 csv-F5 LTM

波斯汪 2023-05-16 16:41:08
我正在尝试审核大量 f5 配置,但我很难解析下面的数据,我尝试修改下面的代码,但它抛出了一个错误。我是 python 的完全菜鸟,这是我第一次自动化这样的任务。谢谢数据:Ltm::Virtual Server: acme.com  Availability     : offline  State            : enabled  Reason           : The children pool member(s) are down  Destination      : 10.1.1.2:80Ltm::Virtual Server: foo.com  Availability     : available  State            : enabled  Reason           : The virtual server is available  Destination      : 10.100.11.15:80Ltm::Virtual Server: hamhamspam.com  Availability     : offline  State            : enabled  Reason           : The children pool member(s) are down  Destination      : 10.200.8.17:443预期产出Virtual Server  Availability    State   Reason                                  Destinationacme.com        offline         enabled The children pool member(s) are down    10.1.1.2:80foo.com         available       enabled The virtual server is available         10.100.11.15:80hamhamspam.com  offline         enabled The children pool member(s) are down    10.200.8.17:443import csvdef convert_to_dict(line, header):    d = {}    for cell in header:        d[cell] = ''    row = line.strip().split(':')    for cell in row:        if cell:            key, value = cell.split(':')            d[key] = value    return ddef extract_fields(logfile):    fields = set()    for line in logfile:        row = line.strip().split(':')        for cell in row:            if cell:                key, value = cell.split(':')                fields.add(key)    logfile.seek(0)    return sorted(list(fields))if __name__ == '__main__':    with open('ltm.txt', 'r') as logfile:        with open('report.csv', 'wb') as csvfile:            csvwriter = csv.writer(csvfile)            header = extract_fields(logfile)            csvwriter.writerow(header)            for line in logfile:                d = convert_to_dict(line, header)                csvwriter.writerow([d[cell] for cell in header])
查看完整描述

2 回答

?
慕运维8079593

TA贡献1876条经验 获得超5个赞

工作代码如下:


lines_data = []

with open('ltm.txt', 'r') as logfile:

    pairs = {}

    for line in logfile:

        new_entry = False

        if line[0].isspace():

          fields = line.strip().split(':')

        else:

          double_colon_idx = line.find('::')

          line = line[double_colon_idx+2:]

          new_entry = True

        fields = line.strip().split(':')

        if new_entry and pairs:

          lines_data.append(pairs)

          pairs = {}

        if len(fields) >= 2:

          key = fields[0]

          value = fields[1]

          pairs[key.strip()] = value.strip()

    lines_data.append(pairs)

headers = lines_data[0].keys()

header_str = ','.join(headers)

with open('report.csv','w') as out:

  out.write(header_str + '\n')

  for entry in lines_data:

    _line = []

    for key in headers:

      _line.append(entry[key])

    out.write(','.join(_line) + '\n')

ltm.txt文件


Ltm::Virtual Server: acme.com

  Availability     : offline

  State            : enabled

  Reason           : The children pool member(s) are down

  Destination      : 10.1.1.2:80

Ltm::Virtual Server: foo.com

  Availability     : available

  State            : enabled

  Reason           : The virtual server is available

  Destination      : 10.100.11.15:80

Ltm::Virtual Server: hamhamspam.com

  Availability     : offline

  State            : enabled

  Reason           : The children pool member(s) are down

  Destination      : 10.200.8.17:443

报告.csv


Virtual Server,Availability,State,Reason,Destination

acme.com,offline,enabled,The children pool member(s) are down,10.1.1.2

foo.com,available,enabled,The virtual server is available,10.100.11.15

hamhamspam.com,offline,enabled,The children pool member(s) are down,10.200.8.17


查看完整回答
反对 回复 2023-05-16
?
喵喵时光机

TA贡献1846条经验 获得超7个赞

您的代码中有不同的问题。


您尝试拆分':',而其中一个键确实包含冒号。你应该分开': '


您始终尝试拆分两次,第一次是行(正确),然后是每个字段(错误)。您应该在拆分后删除一个split并剥离:


 ...

 for line in logfile:

     row = line.split(': ')

     key = row[0].strip()

     fields.add(key)

其他功能相同


您分别处理每一行,而以空格字符开头的行是续行。你应该只提取一对key, value并返回它:


 def extract_pair(line):

     key, value = line.split(': ')

     return key.strip(), value.strip()

然后在你的主要部分,你必须处理续行


     ...

     d = None

     for line in logfile:

         key, value = extract_pair(line)

         if line[0].isspace():

             d[key] = value       # continuation line: update d

         else:

             if d is not None:    # write full previous row

                 csvwriter.writerow([d[cell] for cell in header])

             d = {key: value}     # initial line: reset d

     if d is not None:            # process last row

         csvwriter.writerow([d[cell] for cell in header])


查看完整回答
反对 回复 2023-05-16
  • 2 回答
  • 0 关注
  • 114 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信