1

我有一个 .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 名称?

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

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

4

3 回答 3

9
for line in open('thefile.txt'):
  fields = line.split('|')
  if len(fields) < 4: continue
  if fields[3].trim() != 'FUNC': continue
  dowhateveryouwishwith(line, fields)
于 2009-05-04T06:07:10.657 回答
4

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

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]

很抱歉这么着迷,但我正在努力更好地处理创建列表理解,我认为这值得一试

于 2009-05-04T22:42:35.380 回答
2

这是一个基本的方法。你怎么看?

# 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])
于 2009-05-04T06:06:42.187 回答