我有一个.txt文件,如:

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

如何解析此文件并获取类型为FUNC的函数? 另外,从这个txt我怎么解析和提取.o名称?

我如何通过列式解析或其他方式获得它们。

我需要立即帮助......像往常一样等待合适的解决方案

有帮助吗?

解决方案

我认为这可能比使用正则表达式花费更少,尽管我并不完全清楚你想要完成什么

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]))

我从其他帖子中了解到,当您第一次阅读文件时,包装所有数据会更便宜,更好。因此,如果您想在一次传递中包装整个数据文件,那么您可以执行以下操作

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={}

然后,如果你想要Func类型,你将使用以下内容:

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

很抱歉这么强迫,但我想更好地处理创建列表理解,我觉得这值得一试

其他提示

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

这是一种基本方法。你觉得怎么样?

# 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])
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top