Python XML 文件打开 [英] Python XML File Open
本文介绍了Python XML 文件打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试打开一个 xml 文件并解析它,但是当我尝试打开它时,该文件似乎根本没有打开它只是一直在运行,有什么想法吗?
I am trying to open an xml file and parse it, but when I try to open it the file never seems to open at all it just keeps running, any ideas?
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')
文件是 180.288 KB,为什么它从来没有进入打印部分?
The file is 180.288 KB, why does it never make it to the print portion?
推荐答案
通过一些调整运行 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)
将此示例输入作为 test_file.xml:
<a>
<b>testing 1</b>
<c>testing 2</c>
</a>
产生此输出:
<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'">
注意事项:
- 正如@LukeWoodward 所提到的,避免对大型输入使用基于 DOM 的库,但是 180K 应该没问题.对于 180M,控制可能永远不会从
minidom.parse()
返回,而不会先耗尽内存(MemoryError). - 正如@alecxe 所提到的,您应该消除文件规范中多余的:".您应该已经看到了
IOError: [Errno 22] invalid mode ('r') 或 filename: 'C::/test_file.xml'
的错误输出. - 正如@mzjn 提到的,
xml.dom.minidom.Document
是不可迭代的.您应该已经看到了TypeError:iteration over non-sequence
的错误输出.
- As @LukeWoodward mentioned, avoid DOM-based libraries for large inputs, however 180K should be fine. For 180M, control may never return from
minidom.parse()
without running out of memory first (MemoryError). - As @alecxe mentioned, you should eliminate the extraneous ':' in the file spec. You should have seen error output along the lines of
IOError: [Errno 22] invalid mode ('r') or filename: 'C::/test_file.xml'
. - As @mzjn mentioned,
xml.dom.minidom.Document
is not iterable. You should have seen error output along the lines ofTypeError: iteration over non-sequence
.
这篇关于Python XML 文件打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文