新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:pythonxml解析中文亂碼怎么辦
用python解析XML文件,出現(xiàn)異常信息如下:

創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),福貢企業(yè)網(wǎng)站建設(shè),福貢品牌網(wǎng)站建設(shè),網(wǎng)站定制,福貢網(wǎng)站建設(shè)報價,網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,福貢網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力。可充分滿足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學習、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。
xml.parsers.expat.ExpatError: not well-formed (invalid token): line 13, column 31
相關(guān)推薦:《Python入門教程》
解決方式:
將文件重新編碼成utf-8格式就可以了。
在linux下一種取巧的解決方式:
對于中文,這里選了一種取巧的方法。即先通過iconv命令將xml文件變成utf-8格式,然后將xml中指定編碼格式的內(nèi)容去掉即可。
可參考如下代碼:
cmd = "iconv " + filename + " -t \"utf-8\" >utf.tmp"
os.system(cmd)
utf_filename = "utf.tmp"
f = open(utf_filename)
data = f.read()
f.close()
os.system("rm -f utf.tmp")
#----delete the encode type in xml file-----
#result = re.sub(regex, newstring, subject)
data = data.replace('encoding="GB2312"', '')
data = data.replace('encoding="gb2312"', '')
#self.dom.unlink
self.dom = xml.dom.minidom.parseString( data ) 標題名稱:創(chuàng)新互聯(lián)Python教程:pythonxml解析中文亂碼怎么辦
鏈接分享:http://fisionsoft.com.cn/article/dhhssse.html


咨詢
建站咨詢
