python - Reading text file for specfic keyword included inside brackets '{' -
i read text file below. has geometry names --> " hvac,outlet,inlet,lamelle,duct , wall"
in case 6, may vary depending on different simulation of cfd process.
i extract geometry names , corresponding 'type'. in case geometry , types " hvac,outlet,inlet,lamelle,duct , wall" , "wall , patch" respectively.
should use parse using xml or search string after '{\n' , '}\n' keyword .
geometry { hvac { type wall; ingroups 1(wall); nfaces 904403; startface 38432281; } outlet { type patch; nfaces 8228; startface 39336684; } inlet { type patch; nfaces 347; startface 39344912; } lamelle { type wall; ingroups 1(wall); nfaces 204538; startface 39345259; } duct { type wall; ingroups 1(wall); nfaces 535136; startface 39549797; } wall { type wall; ingroups 1(wall); nfaces 118659; startface 40084933; } }
the answer depends on whether want support whole general openfoam's dictionary format, or not.
if need support format similar have shown in question, simple regex \b(\w+)\s+{\s+type\s+(\w+);
do: https://regex101.com/r/yv8tk2/1 . can option if control how dictionary created, though in case might simpler obtain needed information directly code creates dictionary.
however, openfoam's format dictionaries more rich example. can allow #include
directives, can allow regexs keys, can allow referencing of other keys using $
syntax, can allow comments, c++ code snippets , many more (i not pretend know well). typical example can 2 dictionaries:
---- file data.incl: basetype wall; ---- file data #inputmode merge; #include "data.incl" geometry { /* foo { type wrongtype; // commented entry } */ foo { type $basetype; // expand wall ... } "(bar|buz)" { // match bar , buz ... } }
if need parse such dictionary, recommend code in c++ , use standard openfoam classes, allow in couple of lines of code.
Comments
Post a Comment