1

I am building a file stripper to build a config report, and I have a very very long string as my base data. The following is a very small snippet of it, but it at least illustrates what I'm working with.

Snippet Example: DEFAULT_GATEWAY=192.168.88.1&DELVRY_AGGREGATION_INTERVAL0=1&DELVRY_AGGREGATION_INTERVAL1=1&DELVRY_SCHEDULE0=1&DELVRY_SNI0=192.168.88.158&DELVRY_USE_SSL_TLS1=0&

How would I go about matching the following:

between "DEFAULT_GATEWAY=" and "&"
between "DELVRY_AGGREGATION_INTERVAL0=" and "&"
between "DELVRY_AGGREGATION_INTERVAL1=" and "&"
between "DELVRY_SCHEDULE=" and "&"
between "DELVRY_SNI0=" and "&"
between "DELVRY_USE_SSL_TLS1=" and "&"

and building a dict with it like:

{"DEFAULT_GATEWAY":"192.168.88.1",
 "DELVRY_AGGREGATION_INTERVAL0":"1",
 "DELVRY_AGGREGATION_INTERVAL1":"1",
 "DELVRY_SCHEDULE0":"1",
 "DELVRY_SNI0":"0",
 "DELVRY_USE_SSL_TLS1":"0"}

?

3 Answers 3

3

Here is a way to do it.

In [1]: input = 'DEFAULT_GATEWAY=192.168.88.1&DELVRY_AGGREGATION_INTERVAL0=1&DELVRY_AGGREGATION_INTERVAL1=1&DELVRY_SCHEDULE0=1&DELVRY_SNI0=192.168.88.158&DELVRY_USE_SSL_TLS1=0&'

In [2]: input.split('&')
Out[2]: 
['DEFAULT_GATEWAY=192.168.88.1',
 'DELVRY_AGGREGATION_INTERVAL0=1',
 'DELVRY_AGGREGATION_INTERVAL1=1',
 'DELVRY_SCHEDULE0=1',
 'DELVRY_SNI0=192.168.88.158',
 'DELVRY_USE_SSL_TLS1=0',
 '']

In [3]: [keyval.split('=') for keyval in input.split('&') if keyval]
Out[3]: 
[['DEFAULT_GATEWAY', '192.168.88.1'],
 ['DELVRY_AGGREGATION_INTERVAL0', '1'],
 ['DELVRY_AGGREGATION_INTERVAL1', '1'],
 ['DELVRY_SCHEDULE0', '1'],
 ['DELVRY_SNI0', '192.168.88.158'],
 ['DELVRY_USE_SSL_TLS1', '0']]

In [4]: dict(keyval.split('=') for keyval in input.split('&') if keyval)
Out[4]: 
{'DEFAULT_GATEWAY': '192.168.88.1',
 'DELVRY_AGGREGATION_INTERVAL0': '1',
 'DELVRY_AGGREGATION_INTERVAL1': '1',
 'DELVRY_SCHEDULE0': '1',
 'DELVRY_SNI0': '192.168.88.158',
 'DELVRY_USE_SSL_TLS1': '0'}

Notes

  1. This is the input line
  2. Split by & to get pairs of key-values. Note the last entry is empty
  3. Split each entry by the equal sign while throwing away empty entries
  4. Build a dictionary

Another Solution

In [8]: import urlparse

In [9]: urlparse.parse_qsl(input)
Out[9]: 
[('DEFAULT_GATEWAY', '192.168.88.1'),
 ('DELVRY_AGGREGATION_INTERVAL0', '1'),
 ('DELVRY_AGGREGATION_INTERVAL1', '1'),
 ('DELVRY_SCHEDULE0', '1'),
 ('DELVRY_SNI0', '192.168.88.158'),
 ('DELVRY_USE_SSL_TLS1', '0')]

In [10]: dict(urlparse.parse_qsl(input))
Out[10]: 
{'DEFAULT_GATEWAY': '192.168.88.1',
 'DELVRY_AGGREGATION_INTERVAL0': '1',
 'DELVRY_AGGREGATION_INTERVAL1': '1',
 'DELVRY_SCHEDULE0': '1',
 'DELVRY_SNI0': '192.168.88.158',
 'DELVRY_USE_SSL_TLS1': '0'}
Sign up to request clarification or add additional context in comments.

3 Comments

Beat me to it... Good one. I saw this question late and I was almost done writing this exact solution.
I am lucky this time. Usually, I am pretty late to the game
Perfect and elegant solution. Thanks! This not only gave me the specific key value pairs I wanted, but the entirety of pairs in the config for ease of use.
0

Split first by '&' to get a list of strings, then by '=', like this:

d = dict(kv.split('=') for kv in line.split('&'))

4 Comments

You are missing one check for empty entries.
He just posted a snippet, it's not clear if empty entries are actually possible.
Look at other answers, it is certainly possible. All you are missing is if kv.
Just because he added the check doesn't mean it's possible, the only empty entry is the trailing one, which might just be because it's a snippet.
0
import re

keys = {"DEFAULT_GATEWAY",
    "DELVRY_AGGREGATION_INTERVAL0",
    "DELVRY_AGGREGATION_INTERVAL1",
    "DELVRY_SCHEDULE0",
    "DELVRY_SNI0",
    "DELVRY_USE_SSL_TLS1"}
resdict = {}
for k in keys:
    pat = '{}([^&])&'.format(k)
    mo = re.search(pat, bigstring)
    if mo is None: continue  # no match
    resdict[k] = mo.group(1)

will leave your desired result in resdict, if bigstring is the string you're searching in.

This assumes you know in advance which keys you'll be looking for, and you keep them in a set keys. If you don't know in advance the keys of interest, that's a very different issue of course.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.