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()
posted @ 2019-12-01 13:38  左手编程右手诗  阅读(1665)  评论(0编辑  收藏  举报