简体   繁体   中英

Converting multi-line script output to dictionary using regex

I got the following script output:

***************************************************
[g4u2680c]: searching for domains
---------------------------------------------------
host =   g4u2680c.houston.example.com
         ipaddr = [16.208.16.72]
         VLAN   = [352]
         Gateway= [16.208.16.1]
         Subnet = [255.255.248.0]
         Subnet = [255.255.248.0]
         Cluster= [g4u2679c g4u2680c g9u1484c g9u1485c]

host =   g4u2680c.houston.example.com
         ipaddr = [16.208.16.72]
         VLAN   = [352]
         Gateway= [16.208.16.1]
         Subnet = [255.255.248.0]
         Subnet = [255.255.248.0]
         Cluster= [g4u2679c g4u2680c g9u1484c g9u1485c]

* script completed Mon Jun 15 06:13:14 UTC 2015 **
* sleeping 30 to avoid DOS on dns via a loop **

I need to extract the 2 host list into a dictionary, with out the brackets.

Here is my code:

#!/bin/env python

import re

text="""*************************************************** 
[g4u2680c]: searching for domains
---------------------------------------------------
host =   g4u2680c.houston.example.com
         ipaddr = [16.208.16.72]
         VLAN   = [352]
         Gateway= [16.208.16.1]
         Subnet = [255.255.248.0]
         Subnet = [255.255.248.0]
         Cluster= [g4u2679c g4u2680c g9u1484c g9u1485c]

host =   g4u2680c.houston.example.com
         ipaddr = [16.208.16.72]
         VLAN   = [352]
         Gateway= [16.208.16.1]
         Subnet = [255.255.248.0]
         Subnet = [255.255.248.0]
         Cluster= [g4u2679c g4u2680c g9u1484c g9u1485c]

* script completed Mon Jun 15 06:13:14 UTC 2015 **
* sleeping 30 to avoid DOS on dns via a loop **
***************************************************
"""

seq = re.compile(r"host.+?\n\n",re.DOTALL)

a=seq.findall(text)

matches = re.findall(r'\w.+=.+', a[0])

matches = [m.split('=', 1) for m in matches]

matches = [ [m[0].strip().lower(), m[1].strip().lower()] for m in matches]

#should have function with regular expression to remove bracket here

d = dict(matches)

print d

What I got so far for the first host:

{'subnet': '[255.255.248.0]', 'vlan': '[352]', 'ipaddr': '[16.208.16.72]', 'cluster': '[g4u2679c g4u2680c g9u1484c g9u1485c]', 'host': 'g4u2680c.houston.example.com', 'gateway': '[16.208.16.1]'}

I need help to find the regex to remove the bracket as the value in the dictionary contain data with and without bracket.

Or if there is a better and simpler way to transform the original script output into dictionary.

You can simply use re.findall and dict :

>>> dict([(i,j.strip('[]')) for i,j in re.findall(r'(\w+)\s*=\s*(.+)',text)])
{'Subnet': '255.255.248.0', 'VLAN': '352', 'ipaddr': '16.208.16.72', 'Cluster': 'g4u2679c g4u2680c g9u1484c g9u1485c', 'host': 'g4u2680c.houston.example.com', 'Gateway': '16.208.16.1'}

And about the brackets you can remove them by str.strip method.

You can use: (\\w+)\\s*=\\s*\\[?([^\\n\\]]+)\\]?

demo

import re
p = re.compile(ur'(\w+)\s*=\s*\[?([^\n\]]+)\]?', re.MULTILINE)
test_str = u"host =   g4u2680c.houston.example.com\n         ipaddr = [16.208.16.72]\n         VLAN   = [352]\n         Gateway= [16.208.16.1]\n         Subnet = [255.255.248.0]\n         Subnet = [255.255.248.0]\n         Cluster= [g4u2679c g4u2680c g9u1484c g9u1485c]\n\nhost =   g4u2680c.houston.example.com\n         ipaddr = [16.208.16.72]\n         VLAN   = [352]\n         Gateway= [16.208.16.1]\n         Subnet = [255.255.248.0]\n         Subnet = [255.255.248.0]\n         Cluster= [g4u2679c g4u2680c g9u1484c g9u1485c]\n"

re.findall(p, test_str)

您可以尝试一下。

matches = [m.replace('[','').replace(']','').split('=', 1) for m in matches]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM