我正在尝试在Mac Os El Capitan系统上使用Plistlib在Python 2.7下处理iTunes plist。问题是获得('tracks')返回3,006个显然失去了子键的值。如果我的目标是定位和更改特定跟踪数据,我该如何处理?
正如上一段中埋葬的那样,我需要的确切帮助是:打印跟踪列出了所有子键值,而是不是子键本身。鉴于并非所有键都存在隔离以存在,我如何检索特定 track的钥匙值?
在这里我的测试代码:
import plistlib as pl
plist = pl.readPist('/Users/bryandunphy/Music/iTunes HQR/iTunes Library.xml')
tracks = pl.get('Tracks')
print tracks
这是一个示例数据记录,我认为包含所有可能的子键(如果键为空字符串,false或数字0,则缺少键0):
<key>2655</key>
<dict>
<key>Track ID</key><integer>2655</integer>
<key>Size</key><integer>8558199</integer>
<key>Total Time</key><integer>210120</integer>
<key>Disc Number</key><integer>1</integer>
<key>Disc Count</key><integer>2</integer>
<key>Track Number</key><integer>5</integer>
<key>Track Count</key><integer>17</integer>
<key>Date Modified</key><date>2016-12-15T02:13:07Z</date>
<key>Date Added</key><date>2016-12-14T11:32:46Z</date>
<key>Bit Rate</key><integer>320</integer>
<key>Sample Rate</key><integer>44100</integer>
<key>Persistent ID</key><string>7BD213A791587573</string>
<key>Track Type</key><string>File</string>
<key>File Type</key><integer>1295270176</integer>
<key>File Folder Count</key><integer>5</integer>
<key>Library Folder Count</key><integer>1</integer>
<key>Name</key><string>Born To Be Wild</string>
<key>Artist</key><string>Steppenwolf</string>
<key>Album Artist</key><string>Steppenwolf</string>
<key>Album</key><string>Born To Be Wild: A Retrospective</string>
<key>Genre</key><string>Rock</string>
<key>Kind</key><string>AAC audio file</string>
<key>Sort Album Artist</key><string>Steppenwolf</string>
<key>Location</key><string>file:///Users/bryandunphy/Music/iTunes%20HQR/iTunes%20Media/Music/Steppenwolf/Born%20To%20Be%20Wild_%20A%20Retrospective/1-05%20Born%20To%20Be%20Wild.m4a</string>
</dict>
文件上方的文件顶部是:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Major Version</key><integer>1</integer>
<key>Minor Version</key><integer>1</integer>
<key>Application Version</key><string>12.5.4.42</string>
<key>Date</key><date>2017-01-02T02:41:51Z</date>
<key>Features</key><integer>5</integer>
<key>Show Content Ratings</key><true/>
<key>Library Persistent ID</key><string>4FA8621533B66A9E</string>
<key>Tracks</key>
<dict>
假设您的iTunes plist文件是这样的:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Major Version</key><integer>1</integer>
<key>Minor Version</key><integer>1</integer>
<key>Application Version</key><string>4.6</string>
<key>Music Folder</key>
<string>/Users/bryandunphy/Music/iTunes HQR/iTunes Library.xml</string>
<key>Library Persistent ID</key><string>8E84CC790968E27F</string>
<key>Tracks</key>
<dict>
<key>2655</key>
<dict>
<key>Track ID</key><integer>2655</integer>
<key>Size</key><integer>8558199</integer>
<key>Total Time</key><integer>210120</integer>
<key>Disc Number</key><integer>1</integer>
<key>Disc Count</key><integer>2</integer>
<key>Track Number</key><integer>5</integer>
<key>Track Count</key><integer>17</integer>
<key>Date Modified</key><date>2016-12-15T02:13:07Z</date>
<key>Date Added</key><date>2016-12-14T11:32:46Z</date>
<key>Bit Rate</key><integer>320</integer>
<key>Sample Rate</key><integer>44100</integer>
<key>Persistent ID</key><string>7BD213A791587573</string>
<key>Track Type</key><string>File</string>
<key>File Type</key><integer>1295270176</integer>
<key>File Folder Count</key><integer>5</integer>
<key>Library Folder Count</key><integer>1</integer>
<key>Name</key><string>Born To Be Wild</string>
<key>Artist</key><string>Steppenwolf</string>
<key>Album Artist</key><string>Steppenwolf</string>
<key>Album</key><string>Born To Be Wild: A Retrospective</string>
<key>Genre</key><string>Rock</string>
<key>Kind</key><string>AAC audio file</string>
<key>Sort Album Artist</key><string>Steppenwolf</string>
<key>Location</key><string>file:///Users/bryandunphy/Music/iTunes%20HQR/iTunes%20Media/Music/Steppenwolf/Born%20To%20Be%20Wild_%20A%20Retrospective/1-05%20Born%20To%20Be%20Wild.m4a</string>
</dict>
</dict>
</dict>
</plist>
您的测试代码将输出这样的字典:
{'2655': {'Album': 'Born To Be Wild: A Retrospective', 'Persistent ID': '7BD213A791587573', 'Track Number': 5, 'Album Artist': 'Steppenwolf', 'Track Type': 'File', 'File Folder Count': 5, 'Disc Number': 1, 'Total Time': 210120, 'Artist': 'Steppenwolf', 'Track Count': 17, 'Genre': 'Rock', 'Bit Rate': 320, 'Kind': 'AAC audio file', 'Name': 'Born To Be Wild', 'Sample Rate': 44100, 'Disc Count': 2, 'Sort Album Artist': 'Steppenwolf', 'File Type': 1295270176, 'Track ID': 2655, 'Location': 'file:///Users/bryandunphy/Music/iTunes%20HQR/iTunes%20Media/Music/Steppenwolf/Born%20To%20Be%20Wild_%20A%20Retrospective/1-05%20Born%20To%20Be%20Wild.m4a', 'Date Modified': datetime.datetime(2016, 12, 15, 2, 13, 7), 'Library Folder Count': 1, 'Date Added': datetime.datetime(2016, 12, 14, 11, 32, 46), 'Size': 8558199}}
如果要获得每个曲目的子键值,则可以这样代码:
for id, track_info in tracks.items():
print id, track_info.values()
输出就像:
2655 ['Born To Be Wild: A Retrospective', '7BD213A791587573', 5, 'Steppenwolf', 'File', 5, 1, 210120, 'Steppenwolf', 17, 'Rock', 320, 'AAC audio file', 'Born To Be Wild', 44100, 2, 'Steppenwolf', 1295270176, 2655, 'file:///Users/bryandunphy/Music/iTunes%20HQR/iTunes%20Media/Music/Steppenwolf/Born%20To%20Be%20Wild_%20A%20Retrospective/1-05%20Born%20To%20Be%20Wild.m4a', datetime.datetime(2016, 12, 15, 2, 13, 7), 1, datetime.datetime(2016, 12, 14, 11, 32, 46), 8558199]
您可以改用track_info.items()
,以便获得这样的键值对:
2655 [('Album', 'Born To Be Wild: A Retrospective'), ('Persistent ID', '7BD213A791587573'), ('Track Number', 5), ('Album Artist', 'Steppenwolf'), ('Track Type', 'File'), ('File Folder Count', 5), ('Disc Number', 1), ('Total Time', 210120), ('Artist', 'Steppenwolf'), ('Track Count', 17), ('Genre', 'Rock'), ('Bit Rate', 320), ('Kind', 'AAC audio file'), ('Name', 'Born To Be Wild'), ('Sample Rate', 44100), ('Disc Count', 2), ('Sort Album Artist', 'Steppenwolf'), ('File Type', 1295270176), ('Track ID', 2655), ('Location', 'file:///Users/bryandunphy/Music/iTunes%20HQR/iTunes%20Media/Music/Steppenwolf/Born%20To%20Be%20Wild_%20A%20Retrospective/1-05%20Born%20To%20Be%20Wild.m4a'), ('Date Modified', datetime.datetime(2016, 12, 15, 2, 13, 7)), ('Library Folder Count', 1), ('Date Added', datetime.datetime(2016, 12, 14, 11, 32, 46)), ('Size', 8558199)]