PYTHON提取LOG或TXT特定字段

def get_file_ext(filename):
    arr=os.path.splitext(filename)
    return arr[len(arr) - 1]

import os
import os.path
import re


with open(os.getcwd() + '\\eutrancelltdd_fdd.csv', 'w') as eutrancellfdd:  #change
    eutrancellfdd.write('enbip' + chr(44) + 'MO' + chr(44) + 'cell' + chr(44))
    eutrancellfdd.write('alpha'+chr(44)+'crsGain'+chr(44)+'noOfPucchCqiUsers'+chr(44)+'noOfPucchSrUsers'+chr(44)+'pZeroNominalPucch'+chr(44)+'pZeroNominalPusch'+chr(44)+'pdcchCfiMode'+chr(44))
    eutrancellfdd.write('pdcchCovImproveDtx' + chr(44) + 'pdcchCovImproveQci1' + chr(44) + 'pdcchCovImproveSrb' + chr(44) + 'pdcchLaGinrMargin' + chr(44) + 'pdcchPowerBoostMax' + chr(44) + 'pdcchTargetBler' + chr(44) + 'pdcchTargetBlerVolte' + chr(44))
    eutrancellfdd.write('pdschMaxNrOfPrbsPerUe' + chr(44) + 'pdschTypeBGain' + chr(44) + 'qRxLevMin' + chr(44) + 'qRxLevMinCe' + chr(44) + 'qRxLevMinOffset' + chr(44) + 'ulBlerTargetEnabled' + chr(44) + 'ulHarqVolteBlerTarget' + '\n')
    # qci.write('tReorderingDlPdcp'+chr(59)+'tReorderingUlDiscardPdcp'+chr(59)+'tReorderingUlPdcp'+chr(59)+'dualConnInitUlConf'+chr(59)+'pdcpSNLengthNr\n')
    eutrancellfdd.close


txtpath = os.getcwd()
txttype = '.log'
txtlist = os.listdir(txtpath)
for txt in txtlist: #   text=re.search(r'Struct acBarringForCsfb has 3 members(.*?)\n.*zzzTemporary66( *.*?)',content,re.DOTALL)
    if get_file_ext(txt) == '.log':#regexpr1=re.compile(r"Struct acBarringForCsfb has 3 members(.*?)")

        with open(txtpath +"\\"+ txt)as f,open(os.getcwd()+'\\eutrancelltdd_fdd.csv','a')as eutrancellfdd:

            start = "Struct acBarringForCsfb has 3 members"
            end = "zzzTemporary66"
            file1 = f.readlines()
            linenum = -1
            for line in file1:

                linenum += 1
                if line.find(start)>-1:




                    if (file1[linenum-2]).find("EUtranCellFDD")>-1 and (file1[linenum-2]).find("GZFDD_BB")>-1:


                        site = re.search("EUtranCellFDD=(.*?)\n",(file1[linenum-2]),re.IGNORECASE)
                        #print(txt)
                        #print(site.group(0))
                        eutrancellfdd.write(txt)
                        eutrancellfdd.write(chr(44) + '"'+(file1[linenum-2]).strip('MO').strip()+'"')
                        eutrancellfdd.write(chr(44) + site.group(1))

                        n = 0
                        while n < 450:
                            if (file1[linenum + n]).find(end)==-1 :
                                n += 1
                                # d={}
                                dic = {}
                                key = (file1[linenum + n]).split()[0]
                                value = (file1[linenum + n])
                                # d[y]=z
                                dic[key] = value
                                for keys in dic:
                                    if 'alpha' in keys:
                                        eutrancellfdd.write(chr(44) + dic.get('alpha','alpha').strip('alpha').strip())

                                    if 'crsGain' in keys:
                                        eutrancellfdd.write(chr(44) + dic.get('crsGain', 'crsGain').strip('crsGain').strip())

                                    if 'noOfPucchCqiUsers' in keys:
                                        eutrancellfdd.write(chr(44) + dic.get('noOfPucchCqiUsers', 'noOfPucchCqiUsers').strip('noOfPucchCqiUsers').strip())

                                    if 'noOfPucchSrUsers' in keys:
                                        eutrancellfdd.write(chr(44) + dic.get('noOfPucchSrUsers', 'noOfPucchSrUsers').strip('noOfPucchSrUsers').strip())

                                    if 'pZeroNominalPucch' in keys:
                                        eutrancellfdd.write(chr(44) + dic.get('pZeroNominalPucch').strip('pZeroNominalPucch').strip())

                                    if 'pZeroNominalPusch' in keys:
                                        eutrancellfdd.write(chr(44) + dic.get('pZeroNominalPusch').strip('pZeroNominalPusch').strip())

                                    if 'pdcchCfiMode' in keys:
                                        eutrancellfdd.write(chr(44) + dic.get('pdcchCfiMode').strip('pdcchCfiMode').strip() )
                                    if 'pdcchCovImproveDtx' in keys:
                                        eutrancellfdd.write(chr(44) + dic.get('pdcchCovImproveDtx').strip('pdcchCovImproveDtx').strip() )
                                    if 'pdcchCovImproveQci1' in keys:
                                        eutrancellfdd.write(chr(44) + dic.get('pdcchCovImproveQci1').strip('pdcchCovImproveQci1').strip())
                                    if 'pdcchCovImproveSrb' in keys:
                                        eutrancellfdd.write(chr(44) + dic.get('pdcchCovImproveSrb').strip('pdcchCovImproveSrb').strip())
                                    if 'pdcchLaGinrMargin' in keys:
                                        eutrancellfdd.write(chr(44) + dic.get('pdcchLaGinrMargin').strip('pdcchLaGinrMargin').strip())

                                    if 'pdcchPowerBoostMax' in keys:
                                        eutrancellfdd.write(chr(44) + dic.get('pdcchPowerBoostMax').strip('pdcchPowerBoostMax').strip())
                                    if 'pdcchTargetBler' == keys:
                                        eutrancellfdd.write(chr(44) + dic.get('pdcchTargetBler').strip('pdcchTargetBler').strip())
                                    if 'pdcchTargetBlerVolte' in keys:
                                        eutrancellfdd.write(chr(44) + dic.get('pdcchTargetBlerVolte').strip('pdcchTargetBlerVolte').strip())
                                    if 'pdschMaxNrOfPrbsPerUe' in keys:
                                        eutrancellfdd.write(chr(44) + dic.get('pdschMaxNrOfPrbsPerUe').strip('pdschMaxNrOfPrbsPerUe').strip())
                                    if 'pdschTypeBGain' in keys:
                                        eutrancellfdd.write(chr(44) + dic.get('pdschTypeBGain').strip('pdschTypeBGain').strip())


                                    if 'qRxLevMin' == keys:
                                        eutrancellfdd.write(chr(44) + dic.get('qRxLevMin','qRxLevMin').strip('qRxLevMin').rstrip())
                                    if 'qRxLevMinCe' in keys:
                                       eutrancellfdd.write(chr(44) + dic.get('qRxLevMinCe','qRxLevMinCe').strip('qRxLevMinCe').strip())
                                    if 'qRxLevMinOffset' in keys:
                                       eutrancellfdd.write(chr(44) + dic.get('qRxLevMinOffset','qRxLevMinOffset').strip('qRxLevMinOffset').strip())

                                    if 'ulBlerTargetEnabled' in keys:
                                        eutrancellfdd.write(chr(44) + dic.get('ulBlerTargetEnabled').strip('ulBlerTargetEnabled').strip())

                                    if 'ulHarqVolteBlerTarget' in keys:
                                        eutrancellfdd.write(chr(44) + dic.get('ulHarqVolteBlerTarget').strip('ulHarqVolteBlerTarget').strip()+'\n')

                                    #if 'alpha' in keys:
                                     #   eutrancellfdd.write(chr(44) + dic.get('alpha').strip('alpha').strip()+'\n')

                            else:

                                     break


                    if (file1[linenum-3]).find("EUtranCellFDD")>-1 and (file1[linenum-3]).find("GZLTE")>-1:


                        site = re.search("EUtranCellFDD=(.*?)\n",(file1[linenum-3]),re.IGNORECASE)
                        #print(txt)
                        #print(site.group(0))
                        eutrancellfdd.write(txt)
                        eutrancellfdd.write(chr(44) + '"'+(file1[linenum-3]).strip('MO').strip()+'"')
                        eutrancellfdd.write(chr(44) + site.group(1))

                        n = 0
                        while n < 506:
                            if (file1[linenum + n]).find(end)==-1 :
                                n += 1
                                # d={}
                                dic = {}
                                key = (file1[linenum + n]).split()[0]
                                value = (file1[linenum + n])
                                # d[y]=z
                                dic[key] = value
                                for keys in dic:
                                    if 'alpha' in keys:
                                        eutrancellfdd.write(chr(44) + dic.get('alpha','alpha').strip('alpha').strip())

                                    if 'crsGain' in keys:
                                        eutrancellfdd.write(chr(44) + dic.get('crsGain', 'crsGain').strip('crsGain').strip())

                                    if 'noOfPucchCqiUsers' in keys:
                                        eutrancellfdd.write(chr(44) + dic.get('noOfPucchCqiUsers', 'noOfPucchCqiUsers').strip('noOfPucchCqiUsers').strip())

                                    if 'noOfPucchSrUsers' in keys:
                                        eutrancellfdd.write(chr(44) + dic.get('noOfPucchSrUsers', 'noOfPucchSrUsers').strip('noOfPucchSrUsers').strip())

                                    if 'pZeroNominalPucch' in keys:
                                        eutrancellfdd.write(chr(44) + dic.get('pZeroNominalPucch').strip('pZeroNominalPucch').strip())

                                    if 'pZeroNominalPusch' in keys:
                                        eutrancellfdd.write(chr(44) + dic.get('pZeroNominalPusch').strip('pZeroNominalPusch').strip())

                                    if 'pdcchCfiMode' in keys:
                                        eutrancellfdd.write(chr(44) + dic.get('pdcchCfiMode').strip('pdcchCfiMode').strip() )
                                    if 'pdcchCovImproveDtx' in keys:
                                        eutrancellfdd.write(chr(44) + dic.get('pdcchCovImproveDtx').strip('pdcchCovImproveDtx').strip() )
                                    if 'pdcchCovImproveQci1' in keys:
                                        eutrancellfdd.write(chr(44) + dic.get('pdcchCovImproveQci1').strip('pdcchCovImproveQci1').strip())
                                    if 'pdcchCovImproveSrb' in keys:
                                        eutrancellfdd.write(chr(44) + dic.get('pdcchCovImproveSrb').strip('pdcchCovImproveSrb').strip())
                                    if 'pdcchLaGinrMargin' in keys:
                                        eutrancellfdd.write(chr(44) + dic.get('pdcchLaGinrMargin').strip('pdcchLaGinrMargin').strip())

                                    if 'pdcchPowerBoostMax' in keys:
                                        eutrancellfdd.write(chr(44) + dic.get('pdcchPowerBoostMax').strip('pdcchPowerBoostMax').strip())
                                    if 'pdcchTargetBler' == keys:
                                        eutrancellfdd.write(chr(44) + dic.get('pdcchTargetBler').strip('pdcchTargetBler').strip())
                                    if 'pdcchTargetBlerVolte' in keys:
                                        eutrancellfdd.write(chr(44) + dic.get('pdcchTargetBlerVolte').strip('pdcchTargetBlerVolte').strip())
                                    if 'pdschMaxNrOfPrbsPerUe' in keys:
                                        eutrancellfdd.write(chr(44) + dic.get('pdschMaxNrOfPrbsPerUe').strip('pdschMaxNrOfPrbsPerUe').strip())
                                    if 'pdschTypeBGain' in keys:
                                        eutrancellfdd.write(chr(44) + dic.get('pdschTypeBGain').strip('pdschTypeBGain').strip())


                                    if 'qRxLevMin' == keys:
                                        eutrancellfdd.write(chr(44) + dic.get('qRxLevMin','qRxLevMin').strip('qRxLevMin').rstrip())
                                    if 'qRxLevMinCe' in keys:
                                       eutrancellfdd.write(chr(44) + dic.get('qRxLevMinCe','qRxLevMinCe').strip('qRxLevMinCe').strip())
                                    if 'qRxLevMinOffset' in keys:
                                       eutrancellfdd.write(chr(44) + dic.get('qRxLevMinOffset','qRxLevMinOffset').strip('qRxLevMinOffset').strip())

                                    if 'ulBlerTargetEnabled' in keys:
                                        eutrancellfdd.write(chr(44) + dic.get('ulBlerTargetEnabled').strip('ulBlerTargetEnabled').strip())

                                    if 'ulHarqVolteBlerTarget' in keys:
                                        eutrancellfdd.write(chr(44) + dic.get('ulHarqVolteBlerTarget').strip('ulHarqVolteBlerTarget').strip()+'\n')

                                    #if 'alpha' in keys:
                                     #   eutrancellfdd.write(chr(44) + dic.get('alpha').strip('alpha').strip()+'\n')

                            else:

                                     break


              ######///TDD
                    if (file1[linenum-2]).find("EUtranCellTDD")>-1 and (file1[linenum-2]).find("GZLTE_BB")>-1:



                        site = re.search("EUtranCellTDD=(.*?)\n",(file1[linenum-2]),re.IGNORECASE)
                        #print(txt)
                        #print(site.group(0))
                        eutrancellfdd.write(txt)
                        eutrancellfdd.write(chr(44) + '"'+(file1[linenum-2]).strip('MO').strip()+'"')
                        eutrancellfdd.write(chr(44) + site.group(1))

                        n = 0
                        while n < 460:

                            if (file1[linenum + n]).find(end)==-1 :


                                n += 1
                                # d={}
                                dic = {}
                                key = (file1[linenum + n]).split()[0]
                                value = (file1[linenum + n])
                                # d[y]=z
                                dic[key] = value
                                for keys in dic:
                                    
                                    if 'alpha' in keys:
                                        eutrancellfdd.write(chr(44) + dic.get('alpha','alpha').strip('alpha').strip())

                                    if 'crsGain' in keys:
                                        eutrancellfdd.write(chr(44) + dic.get('crsGain', 'crsGain').strip('crsGain').strip())

                                    if 'noOfPucchCqiUsers' in keys:
                                        eutrancellfdd.write(chr(44) + dic.get('noOfPucchCqiUsers', 'noOfPucchCqiUsers').strip('noOfPucchCqiUsers').strip())

                                    if 'noOfPucchSrUsers' in keys:
                                        eutrancellfdd.write(chr(44) + dic.get('noOfPucchSrUsers', 'noOfPucchSrUsers').strip('noOfPucchSrUsers').strip())

                                    if 'pZeroNominalPucch' in keys:
                                        eutrancellfdd.write(chr(44) + dic.get('pZeroNominalPucch').strip('pZeroNominalPucch').strip())

                                    if 'pZeroNominalPusch' in keys:
                                        eutrancellfdd.write(chr(44) + dic.get('pZeroNominalPusch').strip('pZeroNominalPusch').strip())

                                    if 'pdcchCfiMode' in keys:
                                        eutrancellfdd.write(chr(44) + dic.get('pdcchCfiMode').strip('pdcchCfiMode').strip() )
                                    if 'pdcchCovImproveDtx' in keys:
                                        eutrancellfdd.write(chr(44) + dic.get('pdcchCovImproveDtx').strip('pdcchCovImproveDtx').strip() )
                                    if 'pdcchCovImproveQci1' in keys:
                                        eutrancellfdd.write(chr(44) + dic.get('pdcchCovImproveQci1').strip('pdcchCovImproveQci1').strip())
                                    if 'pdcchCovImproveSrb' in keys:
                                        eutrancellfdd.write(chr(44) + dic.get('pdcchCovImproveSrb').strip('pdcchCovImproveSrb').strip())
                                    if 'pdcchLaGinrMargin' in keys:
                                        eutrancellfdd.write(chr(44) + dic.get('pdcchLaGinrMargin').strip('pdcchLaGinrMargin').strip())

                                    if 'pdcchPowerBoostMax' in keys:
                                        eutrancellfdd.write(chr(44) + dic.get('pdcchPowerBoostMax').strip('pdcchPowerBoostMax').strip())
                                    if 'pdcchTargetBler' == keys:
                                        eutrancellfdd.write(chr(44) + dic.get('pdcchTargetBler').strip('pdcchTargetBler').strip())
                                    if 'pdcchTargetBlerVolte' in keys:
                                        eutrancellfdd.write(chr(44) + dic.get('pdcchTargetBlerVolte').strip('pdcchTargetBlerVolte').strip())
                                    if 'pdschMaxNrOfPrbsPerUe' in keys:
                                        eutrancellfdd.write(chr(44) + dic.get('pdschMaxNrOfPrbsPerUe').strip('pdschMaxNrOfPrbsPerUe').strip())
                                    if 'pdschTypeBGain' in keys:
                                        eutrancellfdd.write(chr(44) + dic.get('pdschTypeBGain').strip('pdschTypeBGain').strip())


                                    if 'qRxLevMin' == keys:
                                        eutrancellfdd.write(chr(44) + dic.get('qRxLevMin','qRxLevMin').strip('qRxLevMin').rstrip())
                                    if 'qRxLevMinCe' in keys:
                                       eutrancellfdd.write(chr(44) + dic.get('qRxLevMinCe','qRxLevMinCe').strip('qRxLevMinCe').strip())
                                    if 'qRxLevMinOffset' in keys:
                                       eutrancellfdd.write(chr(44) + dic.get('qRxLevMinOffset','qRxLevMinOffset').strip('qRxLevMinOffset').strip())

                                    if 'ulBlerTargetEnabled' in keys:
                                        eutrancellfdd.write(chr(44) + dic.get('ulBlerTargetEnabled').strip('ulBlerTargetEnabled').strip())

                                    if 'ulHarqVolteBlerTarget' in keys:
                                        eutrancellfdd.write(chr(44) + dic.get('ulHarqVolteBlerTarget','ulHarqVolteBlerTarget').strip('ulHarqVolteBlerTarget').strip()+'\n')

                                    #if 'alpha' in keys:
                                     #   eutrancellfdd.write(chr(44) + dic.get('alpha').strip('alpha').strip()+'\n')

                            else:

                                     break

                    elif (file1[linenum - 3]).find("EUtranCellTDD") > -1 :

                        site = re.search("EUtranCellTDD=(.*?)\n", (file1[linenum - 3]), re.IGNORECASE)
                        # print(txt)
                        # print(site.group(0))
                        eutrancellfdd.write(txt)
                        eutrancellfdd.write(chr(44) + '"' + (file1[linenum - 3]).strip('MO').strip() + '"')
                        eutrancellfdd.write(chr(44) + site.group(1))

                        n = 0
                        while n < 600:

                            if (file1[linenum + n]).find(end) == -1:

                                n += 1
                                # d={}
                                dic = {}
                                key = (file1[linenum + n]).split()[0]
                                value = (file1[linenum + n])
                                # d[y]=z
                                dic[key] = value
                                for keys in dic:

                                    if 'alpha' in keys:
                                        eutrancellfdd.write(chr(44) + dic.get('alpha', 'alpha').strip('alpha').strip())

                                    if 'crsGain' in keys:
                                        eutrancellfdd.write(
                                            chr(44) + dic.get('crsGain', 'crsGain').strip('crsGain').strip())

                                    if 'noOfPucchCqiUsers' in keys:
                                        eutrancellfdd.write(
                                            chr(44) + dic.get('noOfPucchCqiUsers', 'noOfPucchCqiUsers').strip(
                                                'noOfPucchCqiUsers').strip())

                                    if 'noOfPucchSrUsers' in keys:
                                        eutrancellfdd.write(
                                            chr(44) + dic.get('noOfPucchSrUsers', 'noOfPucchSrUsers').strip(
                                                'noOfPucchSrUsers').strip())

                                    if 'pZeroNominalPucch' in keys:
                                        eutrancellfdd.write(
                                            chr(44) + dic.get('pZeroNominalPucch').strip('pZeroNominalPucch').strip())

                                    if 'pZeroNominalPusch' in keys:
                                        eutrancellfdd.write(
                                            chr(44) + dic.get('pZeroNominalPusch').strip('pZeroNominalPusch').strip())

                                    if 'pdcchCfiMode' in keys:
                                        eutrancellfdd.write(
                                            chr(44) + dic.get('pdcchCfiMode').strip('pdcchCfiMode').strip())
                                    if 'pdcchCovImproveDtx' in keys:
                                        eutrancellfdd.write(
                                            chr(44) + dic.get('pdcchCovImproveDtx').strip('pdcchCovImproveDtx').strip())
                                    if 'pdcchCovImproveQci1' in keys:
                                        eutrancellfdd.write(chr(44) + dic.get('pdcchCovImproveQci1').strip(
                                            'pdcchCovImproveQci1').strip())
                                    if 'pdcchCovImproveSrb' in keys:
                                        eutrancellfdd.write(
                                            chr(44) + dic.get('pdcchCovImproveSrb').strip('pdcchCovImproveSrb').strip())
                                    if 'pdcchLaGinrMargin' in keys:
                                        eutrancellfdd.write(
                                            chr(44) + dic.get('pdcchLaGinrMargin').strip('pdcchLaGinrMargin').strip())

                                    if 'pdcchPowerBoostMax' in keys:
                                        eutrancellfdd.write(
                                            chr(44) + dic.get('pdcchPowerBoostMax').strip('pdcchPowerBoostMax').strip())
                                    if 'pdcchTargetBler' == keys:
                                        eutrancellfdd.write(
                                            chr(44) + dic.get('pdcchTargetBler').strip('pdcchTargetBler').strip())
                                    if 'pdcchTargetBlerVolte' in keys:
                                        eutrancellfdd.write(chr(44) + dic.get('pdcchTargetBlerVolte').strip(
                                            'pdcchTargetBlerVolte').strip())
                                    if 'pdschMaxNrOfPrbsPerUe' in keys:
                                        eutrancellfdd.write(chr(44) + dic.get('pdschMaxNrOfPrbsPerUe').strip(
                                            'pdschMaxNrOfPrbsPerUe').strip())
                                    if 'pdschTypeBGain' in keys:
                                        eutrancellfdd.write(
                                            chr(44) + dic.get('pdschTypeBGain').strip('pdschTypeBGain').strip())

                                    if 'qRxLevMin' == keys:
                                        eutrancellfdd.write(
                                            chr(44) + dic.get('qRxLevMin', 'qRxLevMin').strip('qRxLevMin').rstrip())
                                    if 'qRxLevMinCe' in keys:
                                        eutrancellfdd.write(chr(44) + dic.get('qRxLevMinCe', 'qRxLevMinCe').strip(
                                            'qRxLevMinCe').strip())
                                    if 'qRxLevMinOffset' in keys:
                                        eutrancellfdd.write(
                                            chr(44) + dic.get('qRxLevMinOffset', 'qRxLevMinOffset').strip(
                                                'qRxLevMinOffset').strip())

                                    if 'ulBlerTargetEnabled' in keys:
                                        eutrancellfdd.write(chr(44) + dic.get('ulBlerTargetEnabled').strip(
                                            'ulBlerTargetEnabled').strip())

                                    if 'ulHarqVolteBlerTarget' in keys:
                                        eutrancellfdd.write(
                                            chr(44) + dic.get('ulHarqVolteBlerTarget', 'ulHarqVolteBlerTarget').strip(
                                                'ulHarqVolteBlerTarget').strip() + '\n')

                                    # if 'alpha' in keys:
                                    #   eutrancellfdd.write(chr(44) + dic.get('alpha').strip('alpha').strip()+'\n')

                            else:

                                break
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章