XML解析Python

发布于 2024-12-05 07:24:00 字数 2708 浏览 0 评论 0原文

<dict>
<key>1208</key>
        <dict>
            <key>Track ID</key><integer>1208</integer>
            <key>Name</key><string>Kings And Queens</string>
            <key>Artist</key><string>30 Seconds To Mars</string>
            <key>Album Artist</key><string>30 Seconds to Mars</string>
            <key>Composer</key><string>Jared Leto</string>
            <key>Album</key><string>This Is War</string>
            <key>Genre</key><string>Pop</string>
            <key>Kind</key><string>MPEG audio file</string>
            <key>Size</key><integer>10634388</integer>
            <key>Total Time</key><integer>347820</integer>
            <key>Track Number</key><integer>1</integer>
            <key>Year</key><integer>2009</integer>
            <key>Date Modified</key><date>2011-09-05T21:03:08Z</date>
            <key>Date Added</key><date>2011-08-18T03:57:19Z</date>
            <key>Bit Rate</key><integer>244</integer>
            <key>Sample Rate</key><integer>44100</integer>
            <key>Comments</key><string> 00000000 00000210 000006F0 0000000000EA0000 00000000 00A22291 00000000 00000000 00000000 00000000 00000000 00000000</string>
            <key>Play Count</key><integer>1</integer>

        <key>Play Date</key><integer>3399116673</integer>
            <key>Play Date UTC</key><date>2011-09-17T09:34:33Z</date>
            <key>Persistent ID</key><string>BB3D5E86F5CAC255</string>
            <key>Track Type</key><string>File</string>
            <key>Location</key><string>file://localhost/D:/My%20Music/English%20songs/01-30_seconds_to_mars-kings_and_queens.mp3</string>
            <key>File Folder Count</key><integer>-1</integer>
            <key>Library Folder Count</key><integer>-1</integer>
        </dict>

.....

我想使用 xml.dom.minidom 包来解析这个文件

self.dom = xml.dom.minidom.parse(self.file)
self.name = self.dom.getElementsByTagName('dict')
print self.name[10].firstChild.data

这段代码似乎不起作用。基本上我想要的是检查 dict 的第二个子项的值,然后如果它是轨道,我想要获取轨道的位置。

有没有办法获得满足我条件的 dict 节点?

<dict>
<key>1208</key>
        <dict>
            <key>Track ID</key><integer>1208</integer>
            <key>Name</key><string>Kings And Queens</string>
            <key>Artist</key><string>30 Seconds To Mars</string>
            <key>Album Artist</key><string>30 Seconds to Mars</string>
            <key>Composer</key><string>Jared Leto</string>
            <key>Album</key><string>This Is War</string>
            <key>Genre</key><string>Pop</string>
            <key>Kind</key><string>MPEG audio file</string>
            <key>Size</key><integer>10634388</integer>
            <key>Total Time</key><integer>347820</integer>
            <key>Track Number</key><integer>1</integer>
            <key>Year</key><integer>2009</integer>
            <key>Date Modified</key><date>2011-09-05T21:03:08Z</date>
            <key>Date Added</key><date>2011-08-18T03:57:19Z</date>
            <key>Bit Rate</key><integer>244</integer>
            <key>Sample Rate</key><integer>44100</integer>
            <key>Comments</key><string> 00000000 00000210 000006F0 0000000000EA0000 00000000 00A22291 00000000 00000000 00000000 00000000 00000000 00000000</string>
            <key>Play Count</key><integer>1</integer>

        <key>Play Date</key><integer>3399116673</integer>
            <key>Play Date UTC</key><date>2011-09-17T09:34:33Z</date>
            <key>Persistent ID</key><string>BB3D5E86F5CAC255</string>
            <key>Track Type</key><string>File</string>
            <key>Location</key><string>file://localhost/D:/My%20Music/English%20songs/01-30_seconds_to_mars-kings_and_queens.mp3</string>
            <key>File Folder Count</key><integer>-1</integer>
            <key>Library Folder Count</key><integer>-1</integer>
        </dict>

...

..

I want to use xml.dom.minidom package to parse this file.

self.dom = xml.dom.minidom.parse(self.file)
self.name = self.dom.getElementsByTagName('dict')
print self.name[10].firstChild.data

This code does not seem to work. Basically what I want is to check the value of the second child of dict and then if it is the track I want get the location of the track.

Is there a way to get the dict node which satisfies my conditions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

桃扇骨 2024-12-12 07:24:00

怎么样:

import xml.dom.minidom as minidom
dom = minidom.parse('test.xml')
data={}
for dct in dom.getElementsByTagName('dict'):
    keys=dct.getElementsByTagName('key')
    # key.nextSibling can be an integer or string or date element, or Text node
    # key.nextSibling.firstChild is a Text node or None
    vals=[key.nextSibling.firstChild for key in keys]
    # drill down to the text inside the keys and vals
    keys=[key.firstChild.data for key in keys]
    vals=[val.data if val else None for val in vals]
    data=dict(zip(keys,vals))
    if data['Track ID']=='1208':
        print(data['Location'])
        break

哪个产量

file://localhost/D:/My%20Music/English%20songs/01-30_seconds_to_mars-kings_and_queens.mp3

How about:

import xml.dom.minidom as minidom
dom = minidom.parse('test.xml')
data={}
for dct in dom.getElementsByTagName('dict'):
    keys=dct.getElementsByTagName('key')
    # key.nextSibling can be an integer or string or date element, or Text node
    # key.nextSibling.firstChild is a Text node or None
    vals=[key.nextSibling.firstChild for key in keys]
    # drill down to the text inside the keys and vals
    keys=[key.firstChild.data for key in keys]
    vals=[val.data if val else None for val in vals]
    data=dict(zip(keys,vals))
    if data['Track ID']=='1208':
        print(data['Location'])
        break

which yields

file://localhost/D:/My%20Music/English%20songs/01-30_seconds_to_mars-kings_and_queens.mp3
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文