【问题标题】:Using ElementTree to find <key><string> information使用 ElementTree 查找 <key><string> 信息
【发布时间】:2012-11-19 14:58:05
【问题描述】:

我对 ET 很陌生,但我找不到最简单的方法来做到这一点。我有一个 XML 文件,其中包含未嵌套的关联值,我需要从每个文件中提取文本,并将它们推入稍后将要操作的变量中。问题是知道如何获取每个值。

示例 XML:

<key>Creation Date</key>
    <date>2012-08-18T16:53:23Z</date>
    <key>Entry Text</key>
    <string>This is some entry text</string>
    <key>Location</key>
    <dict>
        <key>Administrative Area</key>
        <string>State</string>
        <key>Country</key>
        <string>United States</string>
        <key>Latitude</key>
        <real>39.647493</real>
        <key>Locality</key>
        <string>City</string>
        <key>Longitude</key>
        <real>-84.11737579840</real>
        <key>Place Name</key>
        <string>123 street</string>
        <key>State</key>
        <string>auto</string>
    </dict>
    <key>Starred</key>
    <false/>
    <key>UUID</key>
    <string>0B38EF885FB84F647GHT738</string>

如您所见,标签并没有真正以智能方式关联。例如我将如何获得:

EntryText = [The <String> Text after <key> Entry Text]

所以 EntryText 会 = 这是一些条目文本

country = [<key>Location <dict> <key> Country <string>]

所以国家=美国

感谢您的宝贵时间。

【问题讨论】:

    标签: python xml parsing elementtree


    【解决方案1】:

    您正在尝试解析OS X .plist XML data structure。 Python 已经带有一个可以为你解析的模块; plistlib。改用它:

    import plistlib
    
    result = plistlib.readPlist(filename)
    print result['Entry Text']
    

    【讨论】:

      猜你喜欢
      • 2021-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多