此篇仅作学习笔记使用,有关xml解析的例子请看:菜鸟教程 Python XML 解析
xml文件的例子:
<?xml version="1.0" encoding="utf-8" ?>
<coll_audit>
<collection id="001">
<collname>SMSC_AO8</collname>
<host>10.0.6.141</host>
<user>bill</user>
<passwd>N-bill01</passwd>
<src_path>/var/opt/nokia/smsc/logdata/auditfiles</src_path>
<src_rule>SMSC_AO8_YYYYMMDD</src_rule>
<dest_rule></dest_rule>
<dest_path>/jzcj3/coll_audit/SMS_NKA/auditfile</dest_path>
</collection>
<collection id="002">
<collname>SMSC_AO9</collname>
<host>10.0.6.142</host>
<user>bill</user>
<passwd>N-bill01</passwd>
<src_path>/var/opt/nokia/smsc/logdata/auditfiles</src_path>
<src_rule>SMSC_AO9_YYYYMMDD</src_rule>
<dest_rule></dest_rule>
<dest_path>/jzcj3/coll_audit/SMS_NKA/auditfile</dest_path>
</collection>
<collection id="003">
<collname>SMSC_AOA</collname>
<host>10.0.6.143</host>
<user>bill</user>
<passwd>N-bill01</passwd>
<src_path>/var/opt/nokia/smsc/logdata/auditfiles</src_path>
<src_rule>SMSC_AOA_YYYYMMDD</src_rule>
<dest_rule></dest_rule>
<dest_path>/jzcj3/coll_audit/SMS_NKA/auditfile</dest_path>
</collection>
<collection id="004">
<collname>SMSC_AOB</collname>
<host>10.0.6.144</host>
<user>bill</user>
<passwd>N-bill01</passwd>
<src_path>/var/opt/nokia/smsc/logdata/auditfiles</src_path>
<src_rule>SMSC_AOB_YYYYMMDD</src_rule>
<dest_rule></dest_rule>
<dest_path>/jzcj3/coll_audit/SMS_NKA/auditfile</dest_path>
</collection>
<collection id="005">
<collname>SMSC_AOE</collname>
<host>10.0.6.151</host>
<user>bill</user>
<passwd>N-bill01</passwd>
<src_path>/var/opt/nokia/smsc/logdata/auditfiles</src_path>
<src_rule>SMSC_AOE_YYYYMMDD</src_rule>
<dest_rule></dest_rule>
<dest_path>/jzcj3/coll_audit/SMS_NKA/auditfile</dest_path>
</collection>
<collection id="006">
<collname>SMSC_AOF</collname>
<host>10.0.6.152</host>
<user>bill</user>
<passwd>N-bill01</passwd>
<src_path>/var/opt/nokia/smsc/logdata/auditfiles</src_path>
<src_rule>SMSC_AOF_YYYYMMDD</src_rule>
<dest_rule></dest_rule>
<dest_path>/jzcj3/coll_audit/SMS_NKA/auditfile</dest_path>
</collection>
<collection id="007">
<collname>SMSC_AOC</collname>
<host>10.0.6.200</host>
<user>bill</user>
<passwd>N-bill01</passwd>
<src_path>/var/opt/nokia/smsc/logdata/auditfiles</src_path>
<src_rule>SMSC_AOC_YYYYMMDD</src_rule>
<dest_rule></dest_rule>
<dest_path>/jzcj3/coll_audit/SMS_NKA/auditfile</dest_path>
</collection>
<collection id="008">
<collname>SMSC_AOD</collname>
<host>10.0.6.201</host>
<user>bill</user>
<passwd>N-bill01</passwd>
<src_path>/var/opt/nokia/smsc/logdata/auditfiles</src_path>
<src_rule>SMSC_AOD_YYYYMMDD</src_rule>
<dest_rule></dest_rule>
<dest_path>/jzcj3/coll_audit/SMS_NKA/auditfile</dest_path>
</collection>
<collection id="009">
<collname>SMSC_AO4</collname>
<host>10.0.6.212</host>
<user>bill</user>
<passwd>N-bill01</passwd>
<src_path>/var/opt/nokia/smsc/logdata/auditfiles</src_path>
<src_rule>SMSC_AO4_YYYYMMDD</src_rule>
<dest_rule></dest_rule>
<dest_path>/jzcj3/coll_audit/SMS_NKA/auditfile</dest_path>
</collection>
<collection id="010">
<collname>SMSC_AO5</collname>
<host>10.0.6.213</host>
<user>bill</user>
<passwd>N-bill01</passwd>
<src_path>/var/opt/nokia/smsc/logdata/auditfiles</src_path>
<src_rule>SMSC_AO5_YYYYMMDD</src_rule>
<dest_rule></dest_rule>
<dest_path>/jzcj3/coll_audit/SMS_NKA/auditfile</dest_path>
</collection>
<collection id="011">
<collname>SMSC_AO6</collname>
<host>10.0.6.214</host>
<user>bill</user>
<passwd>N-bill01</passwd>
<src_path>/var/opt/nokia/smsc/logdata/auditfiles</src_path>
<src_rule>SMSC_AO6_YYYYMMDD</src_rule>
<dest_rule></dest_rule>
<dest_path>/jzcj3/coll_audit/SMS_NKA/auditfile</dest_path>
</collection>
<collection id="012">
<collname>SMSC_AO7</collname>
<host>10.0.6.215</host>
<user>bill</user>
<passwd>N-bill01</passwd>
<src_path>/var/opt/nokia/smsc/logdata/auditfiles</src_path>
<src_rule>SMSC_AO7_YYYYMMDD</src_rule>
<dest_rule></dest_rule>
<dest_path>/jzcj3/coll_audit/SMS_NKA/auditfile</dest_path>
</collection>
</coll_audit>
对应的python解析代码,其实就是定义了相对数量的节点查找对象,从根节点查找到子节点,一般够用了
该脚本实现了解析节点并输出成文本
注意,类似 find("dest_rule").text 方法使用时,需要注意判断节点内容是否为空,返回None。
#! /usr/bin/python # -*- coding: utf-8 -*
from xml.etree import ElementTree #引入ElementTree的包
import logging
import os,sys
import datetime,time
LOG_FORMAT = "%(message)s" #"%(asctime)s %(name)s %(levelname)s %(pathname)s %(message)s "#配置输出日志格式
DATE_FORMAT = '%Y-%m-%d %H:%M:%S %a ' #配置输出时间的格式,注意月份和天数不要搞乱了
LOG_PATH = os.path.join(os.getcwd(),'./logs/readcfg.log')
logging.basicConfig(level=logging.DEBUG,
format=LOG_FORMAT,
datefmt = DATE_FORMAT ,
filemode='a', #覆盖之前的记录 'a'是追加
filename=LOG_PATH #有了filename参数就不会直接输出显示到控制台,而是直接写入文件
)
#定义一个类对象
class CollInfo:
#定义初始化属性,和xml文件属性相同
def __init__(self,collname=None,host=None,user=None,passwd=None,\
src_path=None,src_rule=None,dest_rule=None,dest_path=None):
self.collname=collname
self.host=host
self.user=user
self.passwd=passwd
self.src_path=src_path
self.src_rule=src_rule
self.dest_rule=dest_rule
def __str__(self): #转化为字符串输出
return self.collname + "," \
+ self.host+ ","\
+ self.user+ ','\
+ self.passwd+','\
+ self.src_path+ ','\
+ self.src_rule+ ','\
+ self.dest_rule+','\
+ self.dest_path
def get_yes_year_mon_day():
#获取昨天的日期
current_date = datetime.datetime.now()
yes_time = current_date + datetime.timedelta(days=-1)
yes_year = str(yes_time.year)
yes_month = str(yes_time.month)
yes_day = str(yes_time.day)
if len(yes_month)<2:
yes_month = '0'+yes_month
if len(yes_day)<2:
yes_day = '0'+yes_day
return (yes_year, yes_month, yes_day)
def read_coll_config(cfgfile):
if not os.path.exists(cfgfile):
logging.debug('no such file or directory%s.' %(cfgfile))
return None
rootNode=ElementTree.parse(cfgfile) #parse方法读取xml文件,得到元素树
collcets=rootNode.findall("collection") #findall查询所有的book标签
colls=[]
for collect in collcets: #对得到的所有的根元素下的子标签循环输出
collection=CollInfo() #定义一个类对象
collection.collname=collect.find("collname").text #对象的相应标签值=子标签查找到的固定标签名,并以text形式输出
collection.host=collect.find("host").text
collection.user=collect.find("user").text
collection.passwd=collect.find("passwd").text
collection.src_path=collect.find("src_path").text
collection.src_rule=collect.find("src_rule").text
collection.dest_rule=collect.find("dest_rule").text
#没有本地重命名,以网元侧为准
if None == collection.dest_rule:
collection.dest_rule = collection.src_rule
collection.dest_path=collect.find("dest_path").text
#添加日期
yyyy,mm,dd=get_yes_year_mon_day()
YYYYMMDD=yyyy + mm + dd
dest_path = os.path.join(collection.dest_path, YYYYMMDD)
collection.dest_path = dest_path
colls.append(collection) #将得到的属性值追加到定义的集合中
#输出生成采集配置信息到文件
logging.debug('**************************************************************** %s ****************************************************************'
%(YYYYMMDD))
with open('collinfobyline.txt', 'w') as infoFile:
for coll in colls: #遍历集合
write_str = str(coll) + '\n'
infoFile.writelines(write_str)
logging.debug(coll)
print(coll)
#读取配置文件并生成当日的稽核文件名,目录等信息
if __name__ == '__main__':
cfgfile = os.path.join(os.getcwd(), 'collection.xml')
read_coll_config(cfgfile)
解析的结果: