读取XML并通过Python保留所有字母

时间:2015-06-11 00:11:25

标签: python xml

我有一个看起来像这样的XML:

<data>
    <items>
        <item name="item1"></item>
        <item name="item2"></item>
        <item name="item3"></item>
        <item name="item4"></item>
    </items>
</data>

有谁能告诉我如何阅读文件并打印每一个字母,不仅是内容而且是所有字符(包括括号中的字母)?

1 个答案:

答案 0 :(得分:1)

回到你身边。

import os
filepath='xml_file.xml'
total_bytes=os.stat(filepath).st_size
f = open(filepath,'rb')
for char_index in xrange(total_bytes):
    one_char=f.read(1)
    print "here is the char at offset: %s : %s" % (char_index,one_char)
f.close()