1

I have to perform text parsing using python, The sample text is as below

04h; ParIsa.Front.Area[0].ub_Y (BYTE)
0Dh; ParIsa.Front.Area[0].ub_X (BYTE)
00h; ParIsa.Front.Area[0].ub_P1 (BYTE)
01h; ParIsa.Front.Area[0].ub_P2 (BYTE)
40h; ParIsa.Front.Area[1].ub_Y (BYTE)
0Eh; ParIsa.Front.Area[1].ub_X (BYTE)
00h; ParIsa.Front.Area[1].ub_P1 (BYTE)
01h; ParIsa.Front.Area[1].ub_P2 (BYTE)
03h; ParIsa.Side.Area[0].ub_Y (BYTE)
0Dh; ParIsa.Side.Area[0].ub_X (BYTE)
00h; ParIsa.Side.Area[0].ub_P1 (BYTE)
01h; ParIsa.Side.Area[0].ub_P2 (BYTE)
41h; ParIsa.Side.Area[1].ub_Y (BYTE)
15h; ParIsa.Side.Area[1].ub_X (BYTE)
00h; ParIsa.Side.Area[1].ub_P1 (BYTE)
01h; ParIsa.Side.Area[1].ub_P2 (BYTE)

with such a text, I need to create a data structure in such a way that I can access the individual elements as well as the whole structure, for example

>> Side.Area[0].ub_X
'0x0d'

>> Front.Area
Area[0]
    ub_X = 0x0d
    ub_Y = 0x04
    ub_P1 = 0x00
    ub_P2 = 0x01
Area[1]
    ub_X = 0x0e
    ub_Y = 0x40
    ub_P1 = 0x00
    ub_P2 = 0x01

Accessing the structure as a whole is the difficult part, Can creation of tree be useful here? do you have any suggestions or ideas to implement this, please let me know

0

1 Answer 1

1

Given the homogeneity of your input, a regular expression may be the preferred solution here.

import re
linere = re.compile(r"^(?P<value>[0-9a-fA-F]+)h; ParIsa\.(?P<name>[a-zA-Z.]+)\[(?P<index>\d+)\]\.(?P<attribute>[0-9a-zA-Z_]+) \(BYTE\)$")

Then you can apply the compiled regular expression to every line and access the portions of interest by name on the .groupdict() result of a match object.

Sign up to request clarification or add additional context in comments.

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.