應用內容列表:
1、自定義類
2、使用 log4py 輸出日誌
3、使用 xlwt 處理 Excel 文件
4、使用 lxml 處理 XML 文件
5、使用 profile 檢測內存泄露
# -*- coding: cp936 -*-
import os
import xlwt # write excel
from log4py import Logger,LOGLEVEL_DEBUG # logging
class PreGISData:
xmlRoot = ''
xlsFile = ''
xlsWorkbook = None
xlsSheet = None
xlsStyle = None
log = None
@profile
def __init__(self,root):
self.log = Logger("log4py.conf").get_instance(self)
self.log.get_root().set_loglevel(LOGLEVEL_DEBUG)
self.log.set_target("importXMLToExcel.log")
import datetime
name = str(datetime.date.today()) # '2013-09-27'
self.xmlRoot = root
self.xlsFile = os.getcwd() + os.sep + name + ".xls"
self.log.info("Output File: %s"%self.xlsFile)
@profile
def GetXMLFiles(self,xmlRoot):
# search the folder and get the xml file path
# **foreach xml file, create a thread and parase it. Output the result by thread.
xmlFiles = []
xmldir = os.walk(xmlRoot)
for path,dirs,files in xmldir:
for f in files:
if f.endswith(".xml"):
xmlFiles.append(path + os.sep + f)
return xmlFiles
@profile
def ParseXML(self,xmlFiles):
from lxml import etree
rowid = 0
for f in xmlFiles:
tree = etree.parse(f)
entries = tree.xpath("./Item")
for item in entries:
lampid = item.xpath("./Lampid/text()")[0]
lightid = item.xpath("./Lightid/text()")[0]
boxid = item.xpath("./Boxid/text()")[0]
location = item.xpath("./Location/text()")[0]
self.xlsSheet.write(rowid,0,lampid)
self.xlsSheet.write(rowid,1,lightid)
self.xlsSheet.write(rowid,2,boxid)
self.xlsSheet.write(rowid,3,location)
self.xlsSheet.write(rowid,4,rowid)
rowid += 1
self.xlsWorkbook.save(self.xlsFile)
self.log.info("Create xlsFile finished. Total rows num: %s."%str(rowid))
@profile
def CreateXls(self):
if os.path.exists(self.xlsFile):
os.remove(self.xlsFile)
self.xlsWorkbook = xlwt.Workbook()
self.xlsSheet = self.xlsWorkbook.add_sheet('xml',cell_overwrite_ok=True)
@profile
def main():
try:
xmlRoot = r'E:\Workspace\Project\MyGISData'
gis = PreGISData(xmlRoot)
gis.CreateXls()
xmlFiles = gis.GetXMLFiles(xmlRoot)
gis.ParseXML(xmlFiles)
except BaseException, e:
print str(e)
if __name__ == '__main__':
main()
python -m memory_profiler importXMLToExcel.py -> memory_analysis.txt