免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1488 | 回复: 1
打印 上一主题 下一主题

求教各位大侠一个问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-10-23 19:20 |只看该作者 |倒序浏览
我对python语言一窍不通,从一个同事那里要了一段代码,主要功能就是将GB2312格式的一个xml文件转换成UTF-8格式,但是我怎么运行xml的格式都没有变化,求教各位大侠解释一下这段代码:
#   -*-   encoding:   gb2312   -*-
import os.path
import string
import optparse  
#import ConfigParser
import os
import re
import codecs
from xml.dom.minidom import *
def main():
    p = optparse.OptionParser()
    #-s后面的参数是测试结果源文件   
    p.add_option("-s",dest="TestResultFile")
    #-d后面的参数是统计后输出的目标文件
    p.add_option("-d",dest="DestOutPutFile")
         
    (options, arguments) = p.parse_args()
    #调试打桩
    #options.TestResultFile = "e:\TestResult.xml"   
    #options.DestOutPutFile = "e:\TestResultStatic.xml"
   
    #将GB2312格式转为UTF-8格式   
    f = codecs.open(options.TestResultFile, "rb", "gb2312")
    text = f.read().encode("utf-8")   
    f.close
    f = open(options.TestResultFile, "wb")   
    f.write(text)   
    f.close()   
    #print   text.decode(‘utf-8′).encode(‘gb2312′)   

    try:
     #print "!!!!!Try"
        doc= xml.dom.minidom.parse(options.TestResultFile)
    except:
        #print "!!!!!Except"
        doc= xml.dom.minidom.parse("D:\TestResult.xml")   
    #取出所有的TestCase
    TestCase = doc.getElementsByTagName("TestCase")
    countPassCase  = 0
    countFailCase  = 0
           
    for i in range(len(TestCase)):
        flagTestResult = TestCase.getElementsByTagName("Result")
        if (getText(flagTestResult[0].childNodes) == "Pass"):
            countPassCase = countPassCase + 1
        if (getText(flagTestResult[0].childNodes) == "Fail"):
            countFailCase = countFailCase + 1
    #创建输出文件对象
    implementation = xml.dom.minidom.DOMImplementation()
    document = implementation.createDocument(None,"CI",None)
    fileOuptutXML = document.documentElement
    #创建新的统计标签        
    flagTotalCase = document.createElement("TOTAL")
    flagPassCase  = document.createElement("PASS")
    flagFailCase  = document.createElement("FAIL")
    flagTestDetail= document.createElement("DETAIL")
     
    textTotalCase = document.createTextNode(repr(len(TestCase)))
    textPassCase  = document.createTextNode(repr(countPassCase))
    textFailCase  = document.createTextNode(repr(countFailCase))
   
    flagTotalCase.appendChild(textTotalCase)
    flagPassCase.appendChild(textPassCase)
    flagFailCase.appendChild(textFailCase)
    for j in range(len(TestCase)):
        flagTestDetail.appendChild(TestCase[j])
        
    fileOuptutXML.appendChild(flagTotalCase)
    fileOuptutXML.appendChild(flagPassCase)
    fileOuptutXML.appendChild(flagFailCase)
    fileOuptutXML.appendChild(flagTestDetail)
            
    out=file(options.DestOutPutFile, 'w')
    writer = codecs.lookup('utf-8')[3](out)
    #doc.writexml(writer,indent="",addindent="    ",newl="\n",encoding='utf-8')      
    document.writexml(writer,encoding='utf-8')
    #print "Write to file:"+options.DestOutPutFile
   
def getText(nodelist):
    rc = ""
   
    for node in nodelist:
        if node.nodeType == node.TEXT_NODE:
            #node.normalize()
            for i in range(len(node.data)):
                if not (node.data == '\n' or node.data == ' '):
                    rc = rc + node.data
    return rc
#main()

xml文件:<?xml version="1.0" ?>
<CI>
<TestCase><Name><![CDATA[半速率正常测量报告不功控时BCCH_CBCH测试]]></Name><Result>Fail</Result><TestCaseTime>2009-10-22 19:01:40:392</TestCaseTime></TestCase>
</CI>

论坛徽章:
0
2 [报告]
发表于 2009-10-26 12:23 |只看该作者
我运行了一下
格式已经变了
这段代码前半部分是变更编码
后半部分程序运行有错误没仔细瞧了
不过虽然程序会出错
但是编码确实已经变成UTF-8了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP