Home > Back-end >  python: parse a colon-separated formatted string
python: parse a colon-separated formatted string

Time:10-24

I need to write a python script (I'm a newbie in python but would like to take this a practice) to parse a message of the following format:

T:L:x1:x2:x3:...T1:L1:y1:y2:y3...Tn:Ln:z1:z2:z3:...

where T holds a type, L is the length and x1..xn is the actual data of the type T1-Tn. Each character is separated with : symbol, all values always come in HEX representation.

For example:

1:4:a:5:6:7:2:10:72:75:63:6f:6e:74:72:6f:6c:6c:65:72:2e:6f:72:67

(Type1=1, Length1=4, Type2=2, Length2=16 (10 in hex))

The parsed messages should be stored in dictionary (I think this is the most appropriate data structure, but I'd be glad to hear some other suggestions).

So I am probably going to split the text, extract type and length, walk further and extract L bytes and store them in a dict with T as a key.

  1. So I will run a loop, how do I determine the end of string, so that I can break out of the loop?
  2. The actual data (x1-x3... for example) has to be stored in dictionary with : removed. I'm not sure how to do that.

I'd appreciate to learn about more efficient approach of parsing the string. Thanks!

CodePudding user response:

Something like this should work:

ss = "1:4:a:5:6:7:2:10:72:75:63:6f:6e:74:72:6f:6c:6c:65:72:2e:6f:72:67".split(":")

d = {}
idx = 0
while idx < len(ss):
    key = ss[idx]
    idx  = 1
    length = int(ss[idx])
    idx  = 1
    arr = ss[idx:idx length]
    d[key] = arr
    idx  = length

output d:

{'1': ['a', '5', '6', '7'],
 '2': ['72', '75', '63', '6f', '6e', '74', '72', '6f', '6c', '6c'],
 '65': ['2e', '6f', '72', '67']}

CodePudding user response:

Create an iterator over your string:

message = '1:4:a:5:6:7:2:10:72:75:63:6f:6e:74:72:6f:6c:6c:65:72:2e:6f:72:67'

code = iter(message.split(':'))
data = {}

for t in code:
    l = int(next(code), 16)
    d = [next(code) for _ in range(l)]
    data[t] = d

Output:

>>> data
{'1': ['a', '5', '6', '7'],
 '2': ['72', '75', '63', '6f', '6e', '74', '72', '6f', '6c', '6c', '65', '72', '2e', '6f', '72', '67']}
  • Related