Coding 日常-20170122-XML 配置转 csv

41 篇文章 0 订阅
11 篇文章 0 订阅

调试工具

可劲儿地造吧,伙计!

读写文件

Python 读写文件

解析 XML

SAX & DOM
- SAX (simple API for XML)- 成功提醒榆木脑袋的我有一种方法叫做设置 flag,but,总觉得这并不是最优解法
- DOM (Document Object Model)
- DOM 和 SAX 的区别
DOM 和 SAX 的区别

Python List

Python 列表(List)

遍历对象属性值

百度知道:python如何遍历类内所有数据成员?

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()

连接字符串

+ 与 join 效率比较

其实在实际应用中需要使用字符串连续相加是很少的,更多的操作是几个字符串的连接,当有这样的需求时,使用 + 操作符其实是最快的方式

本人同样也尝试过使用 tList.append(i),最后再 ”.join(tList),但经实际代码对比,还是 + 操作效率更高

元素值是否在列表中

python 中如何判断list中是否包含某个元素

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 到字符串

python中数组,元组,字典和字符串之间的转换

>>> mylist3 = ['w','or','ld']
>>> print ''.join(mylist3)        # List to string
world

以下亲测可用:

def joinListElementToStrWithSymbol(listObj, symbol):
    return symbol.join(listObj)

写 csv 文件,实现 excel 中的单元格内可换行

关于CSV文件格式中的换行问题

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()
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值