python對一個文本的解析


# 定義Tag的簽注
controlAreaStart ="<ControlArea::黃岡>"
controlAreaEnd = "</ControlArea::黃岡>"
entity = "<!Entity=黃岡"
controlAreaStart ="<ControlArea::黃岡>"
controlAreaEnd = "</ControlArea::黃岡>"
baseVoltageStart ="<BaseVoltage::黃岡>"
baseVoltageEnd = "</BaseVoltage::黃岡>"
SubstationStart ="<Substation::黃岡>"
SubstationEnd = "</Substation::黃岡>"
voltageLevelStart ="<VoltageLevel::黃岡>"
voltageLevelEnd = "</VoltageLevel::黃岡>"
bayStart="<Bay::黃岡>"
bayEnd = "</Bay::黃岡>"
breakerStart ="<Breaker::黃岡>"
breakerEnd = "</Breaker::黃岡>"
disconnectorStart ="<Disconnector::黃岡>"
disconnectorEnd = "</Disconnector::黃岡>"
groundDisconnectorStart ="<GroundDisconnector::黃岡>"
groundDisconnectorEnd = "</GroundDisconnector::黃岡>"
busbarSectionStart ="<BusbarSection::黃岡>"
busbarSectionEnd = "</BusbarSection::黃岡>"
aclineStart = "<ACLine::黃岡>"
aclineEnd = "</ACLine::黃岡>"
aCLineSegmentStart ="<ACLineSegment::黃岡>"
aCLineSegmentEnd = "</ACLineSegment::黃岡>"
aCLineDotStart = "<ACLineDot::黃岡>"
aCLineDotEnd = "</ACLineDot::黃岡>"
dCLineSegmentStart = "<DCLineSegment::黃岡>"
dCLineSegmentEnd = "</DCLineSegment::黃岡>"
dCLineDotStart = "<DCLineDot::黃岡>"
dCLineDotEnd = "</DCLineDot::黃岡>"
rectifierInverterStart = "<RectifierInverter::黃岡>"
rectifierInverterEnd = "</RectifierInverter::黃岡>"
#還有一些沒有定義
#獲取標簽在文件中的起始和結束行數
'''
參數 
filePath文件路徑
tagStart:標簽起始值 
tagEnd標簽結束值 
返回值
lineStart:起始行
lineEnd:結束行
'''
def getTagStartEndLineNum(filePath, tagStart, tagEnd):
    if((filePath is not None) and (tagStart is not None) and (tagEnd is not None)):
        f = open(filePath, "r+", encoding='GBK')
        for num, value in enumerate(f, 1):
            if (value.startswith(tagStart)):
                lineStart = num
            if (value.startswith(tagEnd)):
                lineEnd = num
        f.close()
        return lineStart, lineEnd
    else:
        return
#從起始行到結束行的內容
'''
參數 
filePath文件路徑
lineStart:起始行
lineEnd:結束行
返回值
fileComm:返回的內容
'''
def lineCent(filePath,lineStart, lineEnd):
    if((filePath is not None) and (lineStart is not None) and (lineEnd is not None)):
        f = open(filePath, "r+", encoding='GBK')
        fileComm = []
        for line in f.readlines()[lineStart+2:lineEnd-1]:
            print(line)
            fileComm.append(line)
        f.close()
        return fileComm
    else:
        return
'''
下面以測試以aclineStart aclineEnd 的標簽為例
分為三步進行操作
具體操作如下
'''
#1  獲取acline在本文件里面的lineStart:起始行 lineEnd:結束行
fliePath="黃岡_20191126_235500.CIME"
lineStart, lineEnd = getTagStartEndLineNum(fliePath, aclineStart, aclineEnd)#aclineStart aclineEnd 的標簽為例
print(lineStart)
print(lineEnd)
#2 獲取acline在本文件里面的lineStart:起始行 lineEnd:結束行 返回的結果
flieList = lineCent(fliePath,lineStart, lineEnd)
print(flieList)
import sqlite3
conn = sqlite3.connect('test.db')
cursor = conn.cursor()
cursor.execute('''create table ACLine(Num varchar(20),mRID varchar(20),name varchar(20),pathName varchar(20),aclnNum integer)''')
#3 返回的結果進行入庫操作
for line in flieList:
    AA,A, B, C, D,E = line.split(" ")
    print(AA)
    print(A)
    print(B)
    print(C)
    print(D)
    print(E)
    sql = "insert into ACLine(Num,mRID,name,pathName,aclnNum) values(" + A + "," + B + ",'" + C + "','" + D + "'," + E + ')'
    print(sql)
    cursor.execute(sql)
conn.commit()
conn.close()


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM