How To Extract Only Particular Set Of Structs From A File Between Braces In Python
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
Solution 1:
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_struct
found_struct_idx = {}
found_struct = 0
temp_struct = []
final_struct = []
def readfileanddump(filename_and_structnames):
global found_struct
global temp_struct
global final_struct
filename_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 = 1
elif found_struct == 1 and re.match(r'.*}.*', lines):
found_struct = 0
print "Value of temp_struct",temp_struct
final_struct = temp_struct
print "Value of final_struct", final_struct
elif found_struct == 1:
print "Value of lines",lines
lines = str(lines.strip()).replace(";","")
print "Value of lines b4",lines
print "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 = 1
temp_struct = None; temp_struct = []
elif found_struct == 1 and re.match(r'.*}.*', lines):
found_struct = 0
reached_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_struct
print "Value of final_struct",final_struct
elif found_struct == 1:
# print "Value of lines",lines
lines = str(lines.strip()).replace(";","")
# print "Value of lines b4 strip",lines
# print "Value of lines a8 strip",lines.strip()
temp_struct.append(lines)
Post a Comment for "How To Extract Only Particular Set Of Structs From A File Between Braces In Python"