我试图打开一个xml文件并解析它,但当我尝试打开它时,文件似乎永远不会打开它只是一直运行,任何想法?
from xml.dom import minidom
Test_file = open('C::/test_file.xml','r')
xmldoc = minidom.parse(Test_file)
Test_file.close()
for i in xmldoc:
print('test')
Run Code Online (Sandbox Code Playgroud)
该文件是180.288 KB,为什么它永远不会到打印部分?
kjh*_*hes 11
通过一些调整来运行Python代码:
from xml.dom import minidom
Test_file = open('C:/test_file.xml','r')
xmldoc = minidom.parse(Test_file)
Test_file.close()
def printNode(node):
print node
for child in node.childNodes:
printNode(child)
printNode(xmldoc.documentElement)
Run Code Online (Sandbox Code Playgroud)
将此示例输入作为test_file.xml:
<a>
<b>testing 1</b>
<c>testing 2</c>
</a>
Run Code Online (Sandbox Code Playgroud)
产生此输出:
<DOM Element: a at 0xbc56e8>
<DOM Text node "u'\n '">
<DOM Element: b at 0xbc5788>
<DOM Text node "u'testing 1'">
<DOM Text node "u'\n '">
<DOM Element: c at 0xbc5828>
<DOM Text node "u'testing 2'">
<DOM Text node "u'\n'">
Run Code Online (Sandbox Code Playgroud)
笔记:
minidom.parse()没有先耗尽内存(MemoryError)返回.IOError: [Errno 22] invalid mode ('r') or filename: 'C::/test_file.xml'.xml.dom.minidom.Document不可迭代.您应该已经看到了错误输出TypeError: iteration over non-sequence.| 归档时间: |
|
| 查看次数: |
22941 次 |
| 最近记录: |