python - 如何打印对象化元素?

标签 python lxml

我有以下格式的 xml:

<channel>
    <games>
        <game slot='1'>
            <id>Bric A Bloc</id>
            <title-text>BricABloc Hoorah</title-text>
            <link>Fruit Splat</link>
        </game>
    </games>
</channel>

我已经使用 lxml.objectify 解析了这个 xml , 通过:

tree = objectify.parse(file)

可能会有很多 <game>在下面 <games> . 我知道我可以生成 <game> 的列表对象通过:

[ tree.games[0].game[0:4] ]

我的问题是,这些对象是什么类,是否有函数可以打印这些对象属于什么类的任何对象?

最佳答案

也许使用

for game in tree.games[0].game[0:4]:
    print(lxml.objectify.dump(game))

产生

game = None [ObjectifiedElement]
  * slot = '1'
    id = 'Bric A Bloc' [StringElement]
    title-text = 'BricABloc Hoorah' [StringElement]
    link = 'Fruit Splat' [StringElement]

print(game) 显示每个 game 都是一个 lxml.ojectify.ObjectifiedElement .

关于python - 如何打印对象化元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2984665/

相关文章:

python - Django ValidationError 日期格式异常

python - 使用 python 借助命名空间创建 xml

python - 如何在lxml中找到元素的直接子元素

python - 创建返回集合的随机哈希函数

python - 在 Python 中使用 lxml 解析带有错误 header 的文本文件

python - 如何在 lxml 中添加 python 关键字术语作为标记属性?

python - 使用 lxml 和 xpath 抓取网站后出现数据类型问题

Python递归方法调用super

python - 属性错误: 'str' object has no attribute 'readline' While Reading from File

python - 如何将 csv gzip 压缩到小于 100mb?