0

I am working on a file where data with a lot of structures. But I cannot figure out an efficient way to handle all of these. My idea is read line by line and find paratheses in pair. Is there any efficient way to match paratheses then I handle each type in specific logic?

Here is the file I am facing:

.....
# some header info that can be discarded

object node {     
      name R2-12-47-3_node_453;     
      phases ABCN;     
      voltage_A 7200+0.0j;     
      voltage_B -3600-6235j;     
      voltage_C -3600+6235j;     
      nominal_voltage 7200;     
     bustype SWING;
}
...
# a lot of objects node

object triplex_meter {     
      name R2-12-47-3_tm_403;     
      phases AS;     
      voltage_1 120;     
      voltage_2 120;     
      voltage_N 0;     
      nominal_voltage 120;     
}
....
# a lot of object triplex_meter 

object triplex_line {     
      groupid Triplex_Line;
      name R2-12-47-3_tl_409;     
      phases AS;     
      from R2-12-47-3_tn_409;     
      to R2-12-47-3_tm_409;     
      length 30;     
      configuration triplex_line_configuration_1;     
}
...
# a lot of object triplex_meter 

#some nested objects...awh...

So my question is there way to quickly match "{" and "}" so that I can focus on the type inside.

I am expecting some logic like after parsing the file:

 if obj_type == "node":
        # to do 1
 elif obj_type == "triplex_meter":
        # to do 2 

It seems easy to deal with this structure, but I am not sure exactly where to get started.

0

1 Answer 1

1

Code with comments

   file = """

object node {
    name R2-12-47-3_node_453
    phases ABCN
    voltage_A 7200+0.0j
    voltage_B - 3600-6235j
    voltage_C - 3600+6235j
    nominal_voltage 7200
    bustype SWING
}

object triplex_meter {
    name R2-12-47-3_tm_403
    phases AS
    voltage_1 120
    voltage_2 120
    voltage_N 0
    nominal_voltage 120
}


object triplex_line {
    groupid Triplex_Line
    name R2-12-47-3_tl_409
    phases AS
    from R2-12-47-3_tn_409
    to R2-12-47-3_tm_409
    length 30
    configuration triplex_line_configuration_1
}"""

# New python dict
data = {}
# Generate a list with all object taken from file
x = file.replace('\n', '').replace(' - ', ' ').strip().split('object ')
for i in x:
    # Exclude null items in the list to avoid errors
    if i != '':
        # Hard split
        a, b = i.split('{')
        c = b.split(' ')
        # Generate a new list with non null elements
        d = [e.replace('}', '') for e in c if e != '' and e != ' ']
        # Needing a sub dict here for paired values
        sub_d = {}
        # Iterating over list to get paired values
        for index in range(len(d)):
            # We are working with paired values so we unpack only pair indexes
            if index % 2 == 0:
                # Inserting paired values in sub_dict
                sub_d[d[index]] = d[index+1]
        # Inserting sub_dict in main dict "data" using object name
        data[a.strip()] = sub_d
print(data)

Output

{'node': {'name': 'R2-12-47-3_node_453', 'phases': 'ABCN', 'voltage_A': '7200+0.0j', 'voltage_B': '3600-6235j', 'voltage_C': '3600+6235j', 'nominal_voltage': '7200', 'bustype': 'SWING'}, 'triplex_meter': {'name': 'R2-12-47-3_tm_403', 'phases': 'AS', 'voltage_1': '120', 'voltage_2': '120', 'voltage_N': '0', 'nominal_voltage': '120'}, 'triplex_line': {'groupid': 'Triplex_Line', 'name': 'R2-12-47-3_tl_409', 'phases': 'AS', 'from': 'R2-12-47-3_tn_409', 'to': 'R2-12-47-3_tm_409', 'length': '30', 'configuration': 'triplex_line_configuration_1'}}

You can now use the python dict how you want. For e.g.

print(data['triplex_meter']['name'])

EDIT

If you have got lots of "triplex_meter" objects in your file group it in a Python list before inserting them in the main dict

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

3 Comments

Thanks for showing the method. I will see if I can make sense of all of this, and try to understand some of the details shown here!
Wait a sec: there seems to be some error here (when printing data): '3600-6235j': 'voltage_C'. Obviously, the key and value are not exactly right? @Xrayman
x = file.replace('\n', '').replace(' - ', '').strip().split('object ') Becareful to use spaces in second replace and this should work - Also edited in main answer

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.