【问题标题】:Python read xml list by key-valuePython通过键值读取xml列表
【发布时间】:2019-08-15 10:10:05
【问题描述】:

我正在尝试通过 Python 读取报价单。该列表如下所示:

<quotelist
    xmlns="http://www.w3schools.com"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="quotationlist.xsd">
    <quote key = "0">
        <author>Author 0</author>
        <text>Text 0</text>
    </quote>
    <quote key = "1">
        <author>Author 1</author>
        <text>Text 1.</text>
    </quote>
    <quote key = "2">
        <author>Author 2</author>
        <text>Text 2.</text>
    </quote>
</quotelist>

我想把它作为一天一次的报价,因此关键是一年中的哪一天(0 到 364)。 但我很难用 Python 读出第 x 天。

from xml.dom import minidom
dayOfYear = 44 #not relevant, I know how to find this out
mydoc = minidom.parse('./media/quotes.xml')
items = mydoc.getElementsByTagName('quote')
print(items)

这给了我格式中的 365 个引号的列表,这就是我的例外。但是功能是否可以找到带有键号“dayOfYear”的报价?有没有办法不全部加载?那么如何获取作者和文本的值呢?

【问题讨论】:

    标签: python xml attributes minidom


    【解决方案1】:

    您必须自己构建该数据结构。在这种情况下,我选择了嵌套字典:

    items = mydoc.getElementsByTagName('quote')
    output = {int(item.getAttribute('key')): {'author': item.getElementsByTagName('author')[0].firstChild.nodeValue,
                                              'text': item.getElementsByTagName('text')[0].firstChild.nodeValue}
              for item in items}
    
    print(output)
    

    输出

    {0: {'author': 'Author 0',
         'text': 'Text 0'},
     1: {'author': 'Author 1',
         'text': 'Text 1'},
     2: {'author': 'Author 2',
         'text': 'Text 2'}}
    

    然后您可以直接访问您想要的每个“天”,例如output[0]output[1] 等。

    【讨论】:

    • 感谢您的回答!这对我帮助很大,效果很好!
    猜你喜欢
    • 2012-12-10
    • 2020-08-06
    • 1970-01-01
    • 2015-07-16
    • 1970-01-01
    • 2011-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多