使用python

时间:2017-09-23 05:34:45

标签: python

我有一个包含不同路径的巨大文本文件。我必须提取包含.o文件的路径。我这样做,但第一条路径没有被提取。我需要制作一个通用代码来提取这种类型的文件 一块文件如下所示

---------- .note sections ----------

部分.note,第18652节:

00000000类型:未知条目类型

<00> 0000000c名称:Altium

00000014 desc:ltc

00000018类型:未知条目类型

<00> 00000024名称:Altium

<00> 0000002c desc:TASKING TriCore的VX工具集:对象链接器v6.0r1 Build 277

0000006c:SN 07689630

00000078类型:未知条目类型

<00> 00000084名称:Altium

0000008c desc:ltc -o C:\ 00_SIVA \ TRW \ SANDBOX \ DAIMLER_MRAV3_V1.1149 \ BUILD \ TriCore

000000ce:_MM_APP \ TriCore_MM_APP.out -dC:\ 00_Siva \ trw \ sandbox \ Daimler_MRAv3

0000010f:_v1.1149 \ MainMicro \ Application \ Os \ LinkerCmd \ Source \ Linker.lsl

0000014d: - error-limit = 99999 -OCLtxY --output = C:\ 00_SIVA \ TRW \ SANDBOX \ DAIML

0000018e:ER_MRAV3_V1.1149 \ BUILD \ TriCore_MM_APP \ TriCore_MM_APP.sre:STREC:4 -

000001cf:MC:\ 00_SIVA \ TRW \ SANDBOX \ DAIMLER_MRAV3_V1.1149 \ BUILD \ TriCore_MM_AP

00000210:P \ TriCore_MM_APP.mapxml:XML --optimize = -delete-unreferenced-secti

00000251:ons,-first-fit-decrease,+ copytable-compression,+ delete-duplicat

00000292:电子代码,-delete-duplicate-data --map-file = C:\ 00_SIVA \ TRW \ SANDBOX \ D

000002d3:AIMLER_MRAV3_V1.1149 \ BUILD \ TriCore_MM_APP \ TriCore_MM_APP.map

00000310: - map-file --user-provided-initialization-code C:\ 00_SIVA \ TRW \ SAN

00000351:DBOX \ DAIMLER_MRAV3_V1.1149 \ MainMicro \ Application \ SafeTLib \ SBST \ SB

00000392:ST_Kernel_CoreTest.o C:\ 00_SIVA \ TRW \ SANDBOX \ DAIMLER_MRAV3_V1.1149

000003d3:\ MainMicro \ Application \ SafeTLib \ SBST \ SBST_Kernel_ISG.o C:\ 00_SIVA

00000414:\ TRW \ SANDBOX \ DAIMLER_MRAV3_V1.1149 \ MainMicro \ Application \ SafeTLib

00000455:\ SBST \ SBST_Kernel_TestCode.o C:\ 00_SIVA \ TRW \ SANDBOX \ DAIMLER_MRAV3

00000496:_V1.1149 \ MainMicro \ Application \ SafeTLib \ SBST \ SBST_RelocTable.o

import re

ftemp = open(r'D:/temp.txt','w+')
ftest = open(r'D:/test123.txt','w+')
infile = open(r'D:/HLDUMPTC_of_ELF_file.txt','r')
data = infile.readlines()
#print(type(data))
#print(len(data))

actual_data = []
for i in data:
    a = i.rstrip('\n')
    colon_split = a.split(':',1)   
    if(len(colon_split) > 1):
        fdata=colon_split[1].lstrip()
        actual_data.append(fdata)
        #actual_data.append(colon_split[1])


ftemp.writelines(actual_data)
ftemp.close()
infile.close()

ftemp = open(r'D:/temp.txt','r')
temp_data = ftemp.readlines()
#print(type(temp_data))

temp_data1 = [x.lstrip(' ') for x in temp_data]
temp_data2 = [x.rstrip(' ') for x in temp_data1]

line = ''.join(temp_data2)
#print(type(line))
#print(len(line))

nameslist = line.split('.o')
#print(type(nameslist))
#ftest.writelines(nameslist)
#sbreak

ogslist=[]
ogscount = 0

for x in nameslist:
    new = x + '.o'
    newstr1 = new.lstrip(' ')
    newstr = newstr1.replace('. o','.o')
    ogstest = '-Ogs -o' in newstr
    srctest = '.src' in newstr
    if(ogstest == True):
        ogslist.append(newstr)
        ogscount += 1
        continue
    if(srctest == True):
        continue

    if(re.search('^[A-Z][:]',newstr)):

        ftest.write(newstr)
        ftest.write("\n")


#print(ogslist)

for a in ogslist:
    res_spl = a.split('-Ogs -o')
    file1 = res_spl[1].lstrip(' ')
    file = file1.rstrip(' ')
    oend = re.search('.o$',file)
    if(oend == True):
        ftest.write(file)
        ftest.write("\n")

ftest.close()
ftemp.close()

### Final Formatting

fcheck = open(r'D:/test123.txt','r')
ffinal = open(r'D:/out.txt','w+')

check_data = fcheck.readlines()

for s in check_data:
    ccnt = s.count('.o')
    if(ccnt > 1):
        final_split = s.split('.o')
        if(len(final_split) > 0):
            for k in final_split:
                final = k + '.o'
                ffinal.write(final)
                ffinal.write("\n")
    else:
        ffinal.write(s)
        ffinal.write("\n")

ffinal.close()
fcheck.close()

输出: C:\ 00_SIVA \ TRW \ SANDBOX \ DAIMLER_MRAV3_V1.1149 \ MainMicro \应用程序\ SafeTLib \ SBST \ SBST_Kernel_ISG.o

C:\ 00_SIVA \ TRW \ SANDBOX \ DAIMLER_MRAV3_V1.1149 \ MainMicro \应用程序\ SafeTLib \ SBST \ SBST_Kernel_TestCode.o

C:\ 00_SIVA \ TRW \ SANDBOX \ DAIMLER_MRAV3_V1.1149 \ MainMicro \应用程序\ SafeTLib \ SBST \ SBST_RelocTable.o

C:\ 00_SIVA \ TRW \ SANDBOX \ DAIMLER_MRAV3_V1.1149 \ MainMicro \应用程序\ SafeTLib \ SBST \ SBST_TC16E_TestCode.o

C:\ 00_SIVA \ TRW \ SANDBOX \ DAIMLER_MRAV3_V1.1149 \ MainMicro \应用程序\ SafeTLib \ SBST \ SBST_TC16P_CoreTest.o

C:\ 00_SIVA \ TRW \ SANDBOX \ DAIMLER_MRAV3_V1.1149 \ MainMicro \应用程序\ SafeTLib \ SBST \ SBST_TC16P_ISG.o

但输出缺少第一条路径,即C:\ 00_SIVA \ TRW \ SANDBOX \ DAIMLER_MRAV3_V1.1149 \ MainMicro \ Application \ SafeTLib \ SBST \ SBST_Kernel_CoreTest.o

1 个答案:

答案 0 :(得分:0)

假设路径可以在任何地方,但用空格用其他单词分隔:

self.attacking_parts.keys()