调试工具
读写文件
解析 XML
SAX & DOM
- SAX (simple API for XML)- 成功提醒榆木脑袋的我有一种方法叫做设置 flag,but,总觉得这并不是最优解法
- DOM (Document Object Model)
- DOM 和 SAX 的区别
Python List
遍历对象属性值
class A(object):
def __init__(self):
self.a = 1
self.b = 2
def test(self):
for i,j in vars(self).items():
print j
a = A()
a.test()
连接字符串
其实在实际应用中需要使用字符串连续相加是很少的,更多的操作是几个字符串的连接,当有这样的需求时,使用 + 操作符其实是最快的方式
本人同样也尝试过使用 tList.append(i),最后再 ”.join(tList),但经实际代码对比,还是 + 操作效率更高
元素值是否在列表中
theList = ['a','b','c']
if 'a' in theList:
print 'a in the list'
if 'd' not in theList:
print 'd is not in the list'
判断变量类型
python判断变量类型时,为什么不推荐使用type()方法
class A:
pass
class B(A):
pass
isinstance(A(), A) # returns True
type(A()) == A # returns True
isinstance(B(), A) # returns True
type(B()) == A # returns False
unicode str
# 去掉字符串前的 unicode 字符,形如 u''
def escapeUnicode(obj):
strObj = '' + obj
return strObj.encode('utf8')
连接 list 到字符串
>>> mylist3 = ['w','or','ld']
>>> print ''.join(mylist3) # List to string
world
以下亲测可用:
def joinListElementToStrWithSymbol(listObj, symbol):
return symbol.join(listObj)
写 csv 文件,实现 excel 中的单元格内可换行
Quite easy!
# 在带有换行符的字符串两边加双引号就好了!【like, "a\nax\nxx" 即可】
def def joinListElementToStrWithSymbol(listObj, symbol):
return '"' + symbol.join(listObj) + '"'
XML 配置 -> csv
#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
Created on 2017-01-22
@author: zhou
'''
import xml.sax
excelHeader = ['name', 'fromPos', 'toPos', 'source', 'destination', 'application', 'service', 'action']
# print the obj
def printObj(obj):
print '\n'.join(['%s:%s' % item for item in obj.__dict__.items()])
def escapeUnicode(obj):
strObj = '' + obj
return strObj.encode('utf8')
def joinListElementToStrWithSymbol(listObj, symbol):
return '"' + symbol.join(listObj) + '"'
def writeHeader(fileObj):
for value in excelHeader:
fileObj.write(value + ",")
fileObj.write('\n')
# entry obj storage
class Entry:
# construct
def __init__(self):
# ordinary
self.name = ""
# with member
self.fromPos = []
self.toPos = []
self.source = []
self.destination = []
self.application = []
self.service = []
self.action = []
# tag with member
self.fromTag = 0
self.toTag = 0
self.sourceTag = 0
self.destinationTag = 0
self.applicationTag = 0
self.serviceTag = 0
self.actionTag = 0
self.memberTag = 0
self.classTag = 0
# operate with file write
def writeObjToCsv(self, fileObj):
# 一级遍历,即对复合属性不向下继续遍历
for key, value in vars(self).items():
if key in excelHeader:
# 如果是 list 列表类型,需要转换为字符串,再输出到文件
if isinstance(value, list):
value = joinListElementToStrWithSymbol(value, '\n')
# 写入文件中
fileObj.write(escapeUnicode(value) + ',')
fileObj.write('\n')
# configXML
class ConfigXMLHandler( xml.sax.ContentHandler ):
# construct
def __init__(self, fileObj):
self.currentTag = ""
self.entryObj = ""
self.entryObjArray = []
self.rulesTag = 0
self.fileObj = fileObj
# 元素开始事件处理
def startElement(self, tag, attributes):
self.CurrentData = tag
if tag == "rules":
self.rulesTag = 1
elif self.rulesTag and tag == "entry":
self.entryObj = Entry()
self.entryObj.name = attributes["name"]
elif self.entryObj:
if tag == "from":
self.entryObj.fromTag = 1
elif tag == "to":
self.entryObj.toTag = 1
elif tag == "source":
self.entryObj.sourceTag = 1
elif tag == "destination":
self.entryObj.destinationTag = 1
elif tag == "application":
self.entryObj.applicationTag = 1
elif tag == "service":
self.entryObj.serviceTag = 1
elif tag == "action":
self.entryObj.actionTag = 1
elif tag == "member":
self.entryObj.memberTag = 1
elif self.entryObj.actionTag and tag == 'class':
self.entryObj.classTag = 1
# 元素结束事件处理
def endElement(self, tag):
if tag == "rules":
self.rulesTag = 0
elif self.rulesTag and tag == "entry":
# handle with entry here
printObj(self.entryObj)
self.entryObj.writeObjToCsv(self.fileObj)
self.entryObjArray.append(self.entryObj)
self.entryObj = ""
elif self.entryObj:
if tag == "from":
self.entryObj.fromTag = 0
elif tag == "to":
self.entryObj.toTag = 0
elif tag == "source":
self.entryObj.sourceTag = 0
elif tag == "destination":
self.entryObj.destinationTag = 0
elif tag == "application":
self.entryObj.applicationTag = 0
elif tag == "service":
self.entryObj.serviceTag = 0
elif tag == "action":
self.entryObj.actionTag = 0
elif tag == "member":
self.entryObj.memberTag = 0
elif self.entryObj.actionTag and tag == "class":
self.entryObj.classTag = 0
# 内容事件处理
def characters(self, content):
if self.rulesTag:
if self.entryObj:
if self.entryObj.memberTag:
if self.entryObj.fromTag:
self.entryObj.fromPos.append(content)
elif self.entryObj.toTag:
self.entryObj.toPos.append(content)
elif self.entryObj.sourceTag:
self.entryObj.source.append(content)
elif self.entryObj.destinationTag:
self.entryObj.destination.append(content)
elif self.entryObj.applicationTag:
self.entryObj.application.append(content)
elif self.entryObj.serviceTag:
self.entryObj.service.append(content)
elif self.entryObj.classTag:
self.entryObj.action = content
elif isinstance(self.entryObj.action, list) and self.entryObj.actionTag:
self.entryObj.action.append(content)
if ( __name__ == "__main__"):
# 创建一个 XMLReader
parser = xml.sax.make_parser()
# turn off namepsaces
parser.setFeature(xml.sax.handler.feature_namespaces, 0)
# 创建一个文件读写句柄
fileObj = open('output.csv', 'w')
writeHeader(fileObj)
# 重写 ContextHandler
Handler = ConfigXMLHandler(fileObj)
parser.setContentHandler(Handler)
parser.parse("input.xml")
fileObj.close()