我试图在Mac OS El Capitan系统上使用Python 2.7在plistlib上处理iTunes plist。问题是get(' Tracks')返回显然丢失了子密钥的3,006个值。如果我的目标是找到并改变 SPECIFIC 曲目'数据比把plist写回去了?
正如前一段所述,我需要帮助的是:打印曲目列出所有子键值,但不是子键本身。鉴于并非所有密钥都被隔离,我如何检索特定曲目的键值?
下面'我的测试代码:
import plistlib as pl
plist = pl.readPist('/Users/bryandunphy/Music/iTunes HQR/iTunes Library.xml')
tracks = pl.get('Tracks')
print tracks
这是一个示例数据记录,我认为它包含所有可能的子键(如果它们是空字符串,则键不存在,False或数字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>
答案 0 :(得分:0)
假设你的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)]