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.
- So I will run a loop, how do I determine the end of string, so that I can break out of the loop?
- 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!
splitstring method?