python有三种方法解析XML,SAX,DOM,以及ElementTree:
前两种方式解析xml过于繁琐,故使用第三种方式解析
下面是使用xml配置数据库的连接信息来操作数据库
必备的模块 pymysql xml.dom.minidom
代码如下
import pymysql from xml.dom.minidom import parse import xml.dom.minidom def decode_xml(filename): DOMTree = xml.dom.minidom.parse(filename) collection = DOMTree.documentElement databases=collection.getElementsByTagName('database1') print(databases) for database in databases: hostname=database.getElementsByTagName('host')[0].childNodes[0].data user=database.getElementsByTagName('username')[0].childNodes[0].data passwd=database.getElementsByTagName('password')[0].childNodes[0].data port=database.getElementsByTagName('port')[0].childNodes[0].data port=int(port) db=database.getElementsByTagName('database')[0].childNodes[0].data return hostname,user,passwd,port,db if __name__=='__main__': res= decode_xml('./data.xml') host=res[0] user=res[1] passwd=res[2] port=res[3] db=res[4] conn=pymysql.connect(host=host,user=user,password=passwd,db=db,charset='utf8') cursor=conn.cursor() cursor.execute('select * from app_student') res=cursor.fetchall() print(res)
xml的内容如下
python写excel使用的是xlwt模块
下面就是使用xml技术导出数据库表为excel的全部代码
import xlwt import pymysql from xml.dom.minidom import parse import xml.dom.minidom def decode_xml(filename): DOMTree = xml.dom.minidom.parse(filename) collection = DOMTree.documentElement databases=collection.getElementsByTagName('database1') print(databases) for database in databases: hostname=database.getElementsByTagName('host')[0].childNodes[0].data user=database.getElementsByTagName('username')[0].childNodes[0].data passwd=database.getElementsByTagName('password')[0].childNodes[0].data port=database.getElementsByTagName('port')[0].childNodes[0].data port=int(port) db=database.getElementsByTagName('database')[0].childNodes[0].data charset=database.getElementsByTagName('charset')[0].childNodes[0].data tablename=database.getElementsByTagName('tablename')[0].childNodes[0].data outputpath=database.getElementsByTagName('outputpath')[0].childNodes[0].data return hostname,user,passwd,port,db,charset,tablename,outputpath def exportdatabase_excel(host,user,passwd,port,db,charset,tablename,outputpath): conn=pymysql.connect(host=host,user=user,password=passwd,port=port,db=db,charset='utf8') cursor=conn.cursor() cursor.execute('select * from '+tablename) cursor.scroll(0,mode='absolute') res=cursor.fetchall() print(res) fields=cursor.description#获取表头 workbook=xlwt.Workbook() sheet=workbook.add_sheet(tablename,cell_overwrite_ok=True) print(dir(sheet)) list2=[] for field in range(len(fields)) : sheet.write(0,field,fields[field][0]) list2.append(fields[field][0]) list1=[] print(list2) #获取写入数据 for i in range(len(res)): for j in range(len(fields)): if '?' in str(res[i][j]): break else: list1.append(list(res[i])) print(list1) list3=[list2]#list2返回的是一维数组,list1返回的是二维数组,要list2和list1合并则把list2转为二维数组 list3.extend(list1) print(list3) for row in range(0,len(list3)): for col in range(0,len(list3[0])): sheet.write(row,col,list3[row][col]) workbook.save(outputpath) if __name__=='__main__': res = decode_xml('./data.xml') host = res[0] user = res[1] passwd = res[2] port = res[3] db = res[4] charset=res[5] tablename=res[6] outputpath=res[7] exportdatabase_excel(host, user, passwd, port, db, charset, tablename,outputpath)
本文为个人的实际操作经验之谈,转载、复制请注明出处,谢谢!