【问题标题】:How do i get newest date through xpath?我如何通过 xpath 获取最新日期?
【发布时间】:2014-12-13 09:40:50
【问题描述】:

我有下一个 xml:

<Content>
<article title="I Compute, Therefore I am" id="a1">
        <authors>
            <author>Philbert von Cookie</author>
            <author>Alice Brockman</author>
            <author>Pedro Smith</author>
        </authors>
        <journal>
            <name>Journal of Computational Metaphysics</name>
            <volume>3</volume>
            <issue>7</issue>
            <published>04/11/2006</published>
            <pages start="42" end="49"/>
        </journal>
</article>
...
</Content>

根元素内部有很多类似的文章节点->内容

我已将我的 xml 解析为 python 代码并希望获得最大日期值。这是我的python代码:

try:
    import xml.etree.cElementTree as ET
except ImportError:
    import xml.etree.ElementTree as ET

tree = ET.ElementTree(file='data.xml')
root = tree.getroot()
root.tag, root.attrib

我正在尝试使用 iterfind() 来获取它,但目前还不行。

for elem in tree.iterfind('(/*/*/journal/published/value[not(text() < preceding-sibling::value/text()) and not(text() < following-sibling::value/text())])[1]'):
 print (elem.text)

你能帮我回答我如何为 iterfind() 设置我的 XPATH 或者可能有任何其他方法可以做到这一点? 谢谢。

【问题讨论】:

    标签: python xml python-3.x xpath xml-parsing


    【解决方案1】:

    xml.etree.ElementTree 仅提供limited xpath support

    另一种选择是将所有日期解析为一个列表并获取最大值:

    from datetime import datetime
    
    dates = [published.text for published in root.iterfind('.//article/journal/published')]
    print max(dates, key=lambda x: datetime.strptime(x, '%d/%m/%Y'))
    

    请注意,为了在这种情况下找到最大值,您应该比较 datetime 值,而不是字符串(这是 key 函数有帮助的地方)。


    另外,如果要获取最大日期journal对应的记录,可以构造字典映射“日期->日记”,然后获取相应的日记记录:

    from datetime import datetime
    import operator
    
    try:
        import xml.etree.cElementTree as ET
    except ImportError:
        import xml.etree.ElementTree as ET
    
    tree = ET.ElementTree(file='data.xml')
    root = tree.getroot()
    
    mapping = {datetime.strptime(journal.findtext('published'), '%d/%m/%Y'): journal 
               for journal in root.iterfind('.//article/journal')}
    
    journal_latest = max(mapping.iteritems(), key=operator.itemgetter(0))[1]
    print journal_latest.findtext('name')
    

    【讨论】:

    • 我无法运行代码的最后一部分。 Python 说语法错误:Traceback(最近一次调用最后一次):文件“find.py”,第 13 行,在 for journal in root.iterfind('.//article/journal')} 文件“find.py” , 第 13 行, in for journal in root.iterfind('.//article/journal')} 文件 "C:\Python34\lib_strptime.py", 第 500 行, in _strptime_datetime tt, fraction = _strptime(data_string,格式)文件“C:\Python34\lib_strptime.py”,第 337 行,在 _strptime(data_string,格式)中)ValueError:时间数据 '05/25/2002' 与格式 '%d/%m/%Y' 不匹配
    • @DonKorleone 格式不是我所期望的,交换月份和日期:%m/%d/%Y 而不是 %d/%m/%Y
    • 你如何使用 journal.findtext('published')?什么是期刊?如果这是节点的名称,它是在哪里定义的?抱歉,这可能是一个简单的问题,但我是 python 新手。因此,如果您能逐步解释,我将不胜感激
    • 如果我更改日期,它不匹配。所以一开始是对的
    • @DonKorleone 好吧,05/25/2002 显然是%m/%d/%Yjournal.//article/journal xpath 表达式找到的每个结果节点。不知道如何提供更多帮助 - 它适用于您提供的输入。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多