【问题标题】:Read XML and keep all letters through Python [duplicate]读取 XML 并通过 Python 保存所有字母 [重复]
【发布时间】:2015-08-26 11:30:39
【问题描述】:

我有一个如下所示的 XML:

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

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

【问题讨论】:

  • 您能澄清一下您的问题中的“打印”是什么意思吗?重复的问题将读取包含上述内容的文件,包括尖括号内的内容。也许您正试图在网页中显示它,在这种情况下,有必要对将被解释为 HTML/XML 的字符进行转义 - 但您的问题并不清楚。包含您正在使用的不适合的代码并解释问题所在会非常有帮助。
  • 通过打印,我的意思是使用“打印”。原来我将内容类型设置为 text/html 并尝试打印 xml。这就是为什么括号中的所有内容都没有显示的原因。现在我将它设置为文本/文本,它工作得很好。

标签: python xml


【解决方案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()

【讨论】:

    猜你喜欢
    • 2017-03-06
    • 1970-01-01
    • 2011-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多