Parse the .txt file

I have a .txt file, for example:

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 using the FUNC type? Also, from this txt, how can I parse and extract the .o name?

How can I get them by parsing by columns method or how.

I need immediate help ... Waiting for a suitable solution as usual

+1


source to share


3 answers


I think it might cost less than using regexes, although I don't quite understand 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 learned from other posts that it is cheaper and better to wrap all the data when you read the file for the first time. Thus, if you want to complete the entire data file in one pass, you can do the following:

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 to use the Func type, you must use the following:

funcDict={}
for record in fullDict:
    if fullDict[record]['Type']=='FUNC':
        funcDict[record]=fullDict[record]

      

Sorry for being so intrusive, but I'm trying to get better at making lists and I figured it was worthy of a shot

+4


source


for line in open('thefile.txt'):
  fields = line.split('|')
  if len(fields) < 4: continue
  if fields[3].trim() != 'FUNC': continue
  dowhateveryouwishwith(line, fields)

      



+8


source


Here's 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])

      

+2


source







All Articles