分析.txt fi

2024-04-25 20:42:16 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

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


Tags: 文件namefromtxtsizeobjectvaluetype
3条回答

我认为这可能比使用regex要便宜,尽管我不完全清楚你要完成什么

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]

很抱歉,我太执迷不悟了,但我正试图更好地处理创建列表理解,我决定这是值得一试的

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

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

相关问题 更多 >