How to extract only particular set of structs from a file between braces in python

2024/7/8 6:45:07

a. Have a scenario, where in my function reads in a file which contains list of c-structures as shown below, reads the file and extracts all the information between { } braces for each structure and store them in arrays.

package whatstruct;typedef struct packed {int [4:0] version;char party;float parity; char ccc; int [7:0] spend;} party_s;typedef struct packed {int [5:0] cod3;int [1:0] group;int [51:0] duty;char calloff; char selfi;int [11:0] snap; int [5:0] longtrip;int [1:0] whattodo;int [9:0] sky;int [7:0] yahoo;int [5:0] hurrey;int [3:0] appology;int [1:0] temp;int [2:0] iddd;  float dontknow; } dict_s;typedef struct packed {int [4:0] jan;int [12:0] feb;int [1:0] mar;logic mar; logic april; } months_c;typedef struct packed {var apple;int mango;float banana;int lichi} fruits_s; 
endpackage: whatstruct

b. The functions reads in two parameters, one is the file containing the structures and the second parameter contains list of struct names only from which the lines needs to be extracted. For extracting all the infos between {}, tried some of the examples available as given below.

from Tkinter import *
import subprocess
import shlex
import os 
import time
import string
import threading
import sys, argparse
import ttk
import re
import logging
import warnings
import os.path
import gzipreadstructfile = None
filename = None
structnames = Nonedef readfileanddump(filename, structnames):readstructfile = open(filename, "r+")test_str = readstructfile.readlines() matches = re.finditer(r"\{(.*?)\}", str(test_str), re.MULTILINE | re.DOTALL)for matchNum, match in enumerate(matches):for groupNum in range(0, len(match.groups())):print (match.group(1))# for lines in readstructfile:#     print linesreadfileanddump("structpkg.c", all);

c. Was able to extract all the infos between {} and dumpout the infos if the second parameter is provided as "all". But if the second parameter passed is a string containing struct names, only those struct that needs to be extracted. I am unclear how to do that. Is it possible using regexp ? or any other way is better?

Any suggestion is highly helpful !

Updated CODE:

from Tkinter import *
import subprocess
import shlex
import os 
import time
import string
import threading
import sys, argparse
import ttk
from openpyxl import Workbook, load_workbook
from openpyxl.compat import range
from openpyxl.utils import get_column_letter
from openpyxl.styles import Alignment, PatternFill, Border, Color
from openpyxl.styles.colors import YELLOW 
from openpyxl.styles.borders import Border, Side
import re
import logging
import warnings
import os.path
import gzip#import xlwt
#import xlrd readstructfile = None
filename = None
structnames = []
filename_and_structnames_l = [] 
global found_struct
global found_struct_idxfound_struct_idx = {} 
found_struct = 0def readfileanddump(filename_and_structnames):global found_structfilename_and_structnames_l = filename_and_structnames.split(",") if len(filename_and_structnames_l) < 2:filename = filename_and_structnames_l[0]structnames.append('all')print "1. Value of filename %s and structnames %s"%(filename, str(structnames))elif len(filename_and_structnames_l) > 1 and len(filename_and_structnames_l) < 3:filename = filename_and_structnames_l[0]structnames.append(filename_and_structnames_l[1])print "2. Value of filename %s and structnames %s"%(filename, str(structnames))elif len(filename_and_structnames_l) > 2: filename = filename_and_structnames_l[0]for i in range (1, len(filename_and_structnames_l)):structnames.append(filename_and_structnames_l[i])print "3. Value of filename %s and structnames %s"%(filename, str(structnames))if (len(structnames) == 1) and (structnames[0] == 'all'):readstructfile = open(filename, "r+")test_str = readstructfile.readlines() matches = re.finditer(r"\{(.*?)\}", str(test_str), re.MULTILINE | re.DOTALL)for matchNum, match in enumerate(matches):for groupNum in range(0, len(match.groups())):print (match.group(1))# match_group_t = str(str(re.sub('[A-Za-z0-9_[]:]+', '', str(match.group(1)))).strip())# match_group_t = match.groups(1)#for i in range (len(match_group_t)):# print match_group_t[i].replace("\n","")# print str(str(re.sub('[^A-Za-z0-9[:]]+[\r\n]+', ' ', str(match_group_t[i]))))# print re.sub(r"(?<=[a-z])\r?\n"," ", match_group_t[i]) # print ''.join(ch for ch in match_group_t[i] if not ch.isspace())# print ''.join(match_group_t[i].strip().split())#print(re.sub(r"(?:[;\n']|\s{2,})",r'',match_group_t[i])[2:])# print match_group_tprint(re.sub(r"(?:[;\n']|\s{2,})",r'',match.group(1))[2:])else:readstructfile = open(filename, "r+")for lines in readstructfile:if found_struct == 0 and re.match(r'.*typedef struct', lines):found_struct = 1matches = re.finditer(r"\{(.*?)\}.*", str(lines), re.MULTILINE | re.DOTALL)print "Value in matches", matchesif found_struct == 1 and re.match(r'.*}.*', lines):found_struct = 0found_struct_t = str(str(re.sub('[^A-Za-z0-9_]+', ' ', str(lines))).strip()).split(" ")for i in range (len(structnames)):if structnames[i] == found_struct_t[0]:# print "value of found_struct_t", found_struct_t# #found_struct_idx.append(found_struct_t[0]) # found_struct_idx[structnames[i]] = i # print "Value of found_struct_idx", found_struct_idx# breakprint "Value in found_struct_t", found_struct_t # for matchNum, match in enumerate(matches):#     for groupNum in range(0, len(match.groups())):#         print (match.group(1))readfileanddump('alldetailspkg');
Answer

Got It Working As Per My Req:

from Tkinter import *
import subprocess
import shlex
import os 
import time
import string
import threading
import sys, argparse
import ttk
import re
import logging
import warnings
import os.path
import gzip#import xlwt
#import xlrd readstructfile = None
filename = None
structnames = []
filename_and_structnames_l = [] 
global found_struct
global found_struct_idx
global temp_struct
global final_structfound_struct_idx = {} 
found_struct = 0
temp_struct = []
final_struct = [] def readfileanddump(filename_and_structnames):global found_structglobal temp_structglobal final_structfilename_and_structnames_l = filename_and_structnames.split(",") if len(filename_and_structnames_l) < 2:filename = filename_and_structnames_l[0]structnames.append('all')print "1. Value of filename %s and structnames %s"%(filename, str(structnames))elif len(filename_and_structnames_l) > 1 and len(filename_and_structnames_l) < 3:filename = filename_and_structnames_l[0]structnames.append(filename_and_structnames_l[1])print "2. Value of filename %s and structnames %s"%(filename, str(structnames))elif len(filename_and_structnames_l) > 2: filename = filename_and_structnames_l[0]for i in range (1, len(filename_and_structnames_l)):structnames.append(filename_and_structnames_l[i])print "3. Value of filename %s and structnames %s"%(filename, str(structnames))if (len(structnames) == 1) and (structnames[0] == 'all'):readstructfile = open(filename, "r+")for lines in readstructfile:if found_struct == 0 and re.match(r'.*typedef struct', lines):found_struct = 1elif found_struct == 1 and re.match(r'.*}.*', lines):found_struct = 0print "Value of temp_struct",temp_structfinal_struct = temp_structprint "Value of final_struct", final_structelif found_struct == 1:print "Value of lines",lineslines = str(lines.strip()).replace(";","")print "Value of lines b4",linesprint "Value of lines a8",lines.strip()#print(re.sub(r"(?:[;]|\s{2,})",r'',lines)[2:])temp_struct.append(lines)else:readstructfile = open(filename, "r+")for lines in readstructfile:if found_struct == 0 and re.match(r'.*typedef struct', lines):found_struct = 1temp_struct = None; temp_struct = []elif found_struct == 1 and re.match(r'.*}.*', lines):found_struct = 0reached_struct = re.sub(r'.*}.|;',r'',lines)for i in range (len(structnames)):print "Value of structnames[%d] %s and lines is %s"%(i, structnames[i], reached_struct)if str(structnames[i]).strip() == str(reached_struct).strip():for i in range (len(temp_struct)):final_struct.append(temp_struct[i])print "Value of temp_struct",temp_structprint "Value of final_struct",final_structelif found_struct == 1:# print "Value of lines",lineslines = str(lines.strip()).replace(";","")# print "Value of lines b4 strip",lines# print "Value of lines a8 strip",lines.strip()temp_struct.append(lines)
https://en.xdnf.cn/q/120210.html

Related Q&A

Selection of Face of a STL by Face Normal value Threshold

I want to write a script in Python which can generate facegroups in a STL as per the Face Normal value condition. For example, Provided is the snap of Stl, Different colour signifies the face group con…

Python , Changing a font size of a string variable

I have a variable that gets sent to a email as text but the text is all pretty much a standard size with everything the same. I would like to add some emphasis to it as well as make it bigger and make …

Python http.server command gives Syntax Error [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…

If statement to check if the value of a variable is in a JSON file [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Assign variables to a pandas dataframe when specific cell is empty

I am assigning some variables to values from a data frame. The data frame created using this code data = [[tom, 10], ["", 15], [juli, 14]] df = pd.DataFrame(data, columns=[Name, Age])So after…

Try Except for one variable in multiple variables

I am reading every row in a dataframe and assigning its values in each column to the variables The dataframe created using this code data = [[tom, 10], [, 15], [juli, 14]] df = pd.DataFrame(data, colum…

Print method invalid syntax reversing a string [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…

How QRegularExpression can be passed to Qt::MatchRegularExpression

I am trying this sample code that I found which is really really good. I am also trying to figure out the same thing to find an item and scroll to it, but this time I wanted to match the the string whi…

Python list rearrange the elements in a required order

My main list has 44 names as elements. I want to rearrange them in a specific order. I am giving here an example. Note that elements in my actual list are some technical names. No way related to what I…

GMB API accounts.locations.reviews.list

Can i get source code to get reviews using gmb python code also heard mybusiness is discontinued. I tried using my business api is discontinued can i get the implementation process to look for extract…