Reputation: 159261
I have a .txt file like:
Symbols from __ctype_tab.o:
Name Value Class Type Size Line Section
__ctype |00000000| D | OBJECT |00000004| |.data
__ctype_tab |00000000| r | OBJECT |00000101| |.rodata
Symbols from _ashldi3.o:
Name Value Class Type Size Line Section
__ashldi3 |00000000| T | FUNC |00000050| |.text
How can i parsr this file and get the functions with type FUNC ? Also,from this txt how can i parse and extract .o name ?
How can i get them by column wise parsing or else how.
I need an immediate help...Waiting for an appropriate solution as usual
Upvotes: 1
Views: 14076
Reputation: 4940
I think this might cost less than the use of regexes though i am not totally clear on what you are trying to accomplish
symbolList=[]
for line in open('datafile.txt','r'):
if '.o' in line:
tempname=line.split()[-1][0:-2]
pass
if 'FUNC' not in line:
pass
else:
symbolList.append((tempname,line.split('|')[0]))
I have learned from other posts it is cheaper and better to wrap up all of the data when you are reading through a file the first time. Thus if you wanted to wrap up the whole datafile in one pass then you could do the following instead
fullDict={}
for line in open('datafile.txt','r'):
if '.o' in line:
tempname=line.split()[-1][0:-2]
if '|' not in line:
pass
else:
tempDict={}
dataList=[dataItem.strip() for dataItem in line.strip().split('|')]
name=dataList[0].strip()
tempDict['Value']=dataList[1]
tempDict['Class']=dataList[2]
tempDict['Type']=dataList[3]
tempDict['Size']=dataList[4]
tempDict['Line']=dataList[5]
tempDict['Section']=dataList[6]
tempDict['o.name']=tempname
fullDict[name]=tempDict
tempDict={}
Then if you want the Func type you would use the following:
funcDict={}
for record in fullDict:
if fullDict[record]['Type']=='FUNC':
funcDict[record]=fullDict[record]
Sorry for being so obsessive but I am trying to get a better handle on creating list comprehensions and I decided that this was worthy of a shot
Upvotes: 4
Reputation: 73702
Here is a basic approach. What do you think?
# Suppose you have filename "thefile.txt"
import re
obj = ''
for line in file('thefile.txt'):
# Checking for the .o file
match = re.search('Symbols from (.*):', line)
if match:
obj = match.groups()[0]
# Checking for the symbols.
if re.search('|', line):
columns = [x.strip() for x in a.split('|')]
if columns[3] == 'FUNC':
print 'File %s has a FUNC named %s' % (obj, columns[0])
Upvotes: 2
Reputation: 881497
for line in open('thefile.txt'):
fields = line.split('|')
if len(fields) < 4: continue
if fields[3].trim() != 'FUNC': continue
dowhateveryouwishwith(line, fields)
Upvotes: 9