【求助】python xml文件读取和转换问题
#将GB2312格式转为UTF-8格式
f = codecs.open('e:\TestResult.xml', "rb", "gb2312")
text = f.read().encode("utf-8")
f.close
f = open(options.TestResultFile, "wb")
f.write(text)
f.close()
提示 File "E:\TOOLS\ICP\ICP-23\cruisecontrol\userData\groups\group01\projects\R12ProductTest\script\pyt
hon_script\StaticResult.py", line 29, in main
text = f.read().encode("utf-8")
File "C:\Python25\lib\codecs.py", line 606, in read
return self.reader.read(size)
UnicodeDecodeError: 'gb2312' codec can't decode bytes in position 55-56: illegal multibyte sequence
是f.read()出了问题,因为。encode("utf-8") 去掉仍然是该现象。
不知道我这里读取有何问题?
这是成熟的产品代码,已经有别人在使用。而且我使用他们同样的文件来操作,以及同样的python版本(2.5)也是这样的问题,好奇怪!
对了,忘了 open
Python code:
def _openAnything(self, source):
try:
f = urllib.urlopen(source)
data = f.read()
相关问答:
如何对xml内容数据进行比较
例如:
<?xml version='1.0' encoding='utf-8'?>
<TestLIST>
<PID>40</PID>
<TestName>深圳市</TestName>
......
采用DOM 进行解析xml。要求将xml字串插入指定节点作为其子节点
比如:
原xml
<root>
</root>
要插入的xml字串
<a>test</a>
目的xml:
<root>
<a> ......
string b = "";
b = System.Guid.NewGuid().ToString();//获得会话ID
//写XML
XmlDocument xml = new XmlDocumen ......
想用XML加FLASH 做统计报表 ,,有没有人有好的建议呀~~就是把XML里的内容导入到做好的FLASH模块里面去。。。
帮顶
你没用过开源的OpenFlashChart吗?
试试FusionChart
数据源就是XML的
http://www.fusionc ......