python读取xml文件时的问题

py文件#!/usr/bin/envpython#coding=utf-8fromxml.dom.minidomimportparse,parseStringdefget... py文件
#!/usr/bin/env python
#coding=utf-8
from xml.dom.minidom import parse, parseString

def getText(nodelist):
rc =''
for node in nodelist:
if node.nodeType == node.TEXT_NODE:
rc = rc + node.data
return rc

def readConfig(filename):
dom1 = parse(filename) # parse an XML file by name

config_element = dom1.getElementsByTagName('config')[0]
routelist = config_element.getElementsByTagName('routelist')[0]
route = config_element.getElementsByTagName('route')[0]
ip = config_element.getElementsByTagName('ip')[0]
port = config_element.getElementsByTagName('port')[0]
delay = config_element.getElementsByTagName('delay')[0]

return (getText(routelist.childNodes),getText(route.childNodes),getText(ip.childNodes),getText(port.childNodes),getText(delay.childNodes))

str1,str2,str3,str4,str5 = readConfig('xf.xml')
print str1,str2,str3,str4,str5

xml文件
<?xml version=”1.0″ encoding=”utf-8″?>
<config>
<routelist>routelist</routelist>
<route>route</route>
<ip>ip</ip>
<port>port</port>
<delay>delay</delay>
</config>

enca命令查看xml文件的编码格式:
Universal transformation format 8 bits; UTF-8

然后python rc.py
出现问题:
Traceback (most recent call last):
File "rc.py", line 25, in <module>
str1,str2,str3,str4,str5 = readConfig('xf.xml')
File "rc.py", line 13, in readConfig
dom1 = parse(filename) # parse an XML file by name
File "/usr/lib/python2.6/xml/dom/minidom.py", line 1918, in parse
return expatbuilder.parse(file)
File "/usr/lib/python2.6/xml/dom/expatbuilder.py", line 924, in parse
result = builder.parseFile(fp)
File "/usr/lib/python2.6/xml/dom/expatbuilder.py", line 207, in parseFile
parser.Parse(buffer, 0)
xml.parsers.expat.ExpatError: XML declaration not well-formed: line 1, column 14
我三个地方都统一用了UTF-8啊。。。。怎么还出这样的错误啊。。。。真是无语。。。。。。。
展开
 我来答
lix_xu
2011-10-07 · TA获得超过1213个赞
知道小有建树答主
回答量:823
采纳率:0%
帮助的人:697万
展开全部
把这行的引号都换成英文的。
<?xml version=”1.0″ encoding=”utf-8″?>
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式