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啊。。。。怎么还出这样的错误啊。。。。真是无语。。。。。。。 展开
#!/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啊。。。。怎么还出这样的错误啊。。。。真是无语。。。。。。。 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询