解析 .txt 文件

发布于 2024-07-19 00:24:41 字数 621 浏览 5 评论 0原文

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

我如何通过按列解析来获取它们或者如何获取它们。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

撑一把青伞 2024-07-26 00:24:41
for line in open('thefile.txt'):
  fields = line.split('|')
  if len(fields) < 4: continue
  if fields[3].trim() != 'FUNC': continue
  dowhateveryouwishwith(line, fields)
for line in open('thefile.txt'):
  fields = line.split('|')
  if len(fields) < 4: continue
  if fields[3].trim() != 'FUNC': continue
  dowhateveryouwishwith(line, fields)
旧城空念 2024-07-26 00:24:41

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

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]

抱歉,我这么着迷,但我正在尝试更好地处理创建列表理解,我决定这值得一试

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

夜司空 2024-07-26 00:24:41

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

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

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])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文