Python?plist文件的讀取方式
更新時間:2025年07月18日 11:12:16 作者:caron4
這篇文章給大家介紹Python?plist文件的讀取方式,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧
Python練習:讀取Apple Plist文件
Plist文件簡介
- ??定義??:Apple公司創(chuàng)建的基于XML結構的文件格式
- ??特點??:采用XML語法組織數(shù)據,可存儲鍵值對、數(shù)組等結構化信息
- 文件擴展名??:.plist
- 應用場景:
??iOS系統(tǒng):?? 應用描述文件(權限配置:藍牙/網絡訪問/GameCenter等)IPA安裝包中的配置信息 ??
macOS系統(tǒng)??: .app應用的配置文件 系統(tǒng)偏好設置存儲 ??
蘋果生態(tài)?? iTunes音樂播放列表 Xcode項目配置
Python操作庫:plistlib
import plistlib
代碼適配指南(Python 2 → Python 3)
??廢棄方法??(Python 2舊版)
plist = plistlib.read(filename)
??正確方法??(Python 3新版)
with open(filename, 'rb') as plist_file:
plist = plistlib.load(plist_file)注意事項
- 始終使用??二進制模式??(‘rb’)打開文件 Python
- 3.4+ 支持load()/dump()替代舊版API
- macOS內置plutil工具可驗證文件有效性:bash 下運行下面的代碼
plutil -lint Example.plist
下文是練習代碼,其中也包含了畫圖部分
練習代碼:
import re, argparse
import sys
from matplotlib import pyplot
import plistlib
import numpy as np
# 找到重復的音樂
def find_duplicates(file_name):
print("Find duplicate tracks in %s..." % file_name)
with open(file_name, 'rb') as f:
plist = plistlib.load(f)
# 讀取playlist
# 獲取音軌目錄
tracks = plist['Tracks']
# 創(chuàng)建音軌字典
track_names = {}
# 結構:{name:(duration,count)}
# 遍歷 添加
for track_id, track in tracks.items():
try:
name = track['name']
duration = track['Total Time']
# 檢查是不是以及在在字典里面了
if name in track_names:
# 匹配 歌曲名稱和時長
if duration // 1000 == track_names['name'][0] // 1000:
count = track_names[name][1]
track_names[name] = (duration, count + 1)
else:
# 不匹配的情況下
track_names[name] = (duration, count)
except:
# ignore
pass
# 保存重復的音軌, name, count
dups = []
for k, v in track_names.items():
if v[1] > 1:
dups.append(k, v[1])
# 保存到一個文件
if len(dups) > 0:
print("發(fā)現(xiàn)一共有%d個重復的文件, 以及保存到了dup.txt中" % len(dups))
else:
print("沒有發(fā)現(xiàn)任何重復的文件")
f = open("dups.txt", "w")
for val in dups:
f.writable("[%d] %s \n" % (val[0], val[1]))
f.close()
# 查找多個播放列表中的共同的音軌
def find_common_tracks(file_names):
# a list of sets of track names
track_name_sets = []
# 遍歷讀取多個plist文件
for file_name in file_names:
track_names = set()
with open(file_name, 'rb') as f:
plist = plistlib.load(f)
# 獲取音軌節(jié)點
tracks = plist.get("Tracks", {})
# 遍歷迭代
for track_id, track in tracks.items():
try:
track_names.add(track['Name'])
except:
# ignore
pass
# 添加到track_name_sets中
track_name_sets.append(track_names)
# 交集處理
common_tracks = set.intersection(*track_name_sets)
# 寫文件
if len(common_tracks) > 0:
f = open("common.txt", "w")
for val in common_tracks:
# s = "%s\n" % val
f.write(f"{val}\n")
f.close()
print(f"Track names wirte to common.txt { len(common_tracks)}")
else:
print("No common tracks!")
# 收集歌曲評分和時長
def plot_stats(file_name):
# 讀取播放列表
with open(file_name, 'rb') as f:
plist = plistlib.load(f)
tracks = plist['Tracks']
# 創(chuàng)建音軌排序和時長
ratings = []
durations = []
for track_id, track in tracks.items():
try:
ratings.append(track['Album Rating'])
durations.append(track['Total Time'])
except:
# ignore
pass
# 確保是有效數(shù)據
if ratings == [] or durations == []:
print(f"在文件中%s沒有有效的Album Rating/Total Time 數(shù)據 {file_name}")
return
# scatter plot
x= np.array(durations, np.int32)
# convert to minutes
x = x/60000.0
y = np.array(ratings, np.int32)
pyplot.subplot(2, 1, 1)
pyplot.plot(x, y, 'o')
pyplot.axis([0, 1.05*np.max(x), -1, 110])
pyplot.xlabel('Track duration')
pyplot.ylabel('Track rating')
# plot histogram
pyplot.subplot(2, 1, 2)
pyplot.hist(x, bins=20)
pyplot.xlabel('Track duration')
pyplot.ylabel('Count')
# show plot
pyplot.show()
def main():
# create parser
descStr = """
This program analyzes playlist files (.xml) exported from iTunes.
"""
parser = argparse.ArgumentParser(description=descStr)
# add a mutually exclusive group of arguments
group = parser.add_mutually_exclusive_group()
# add expected arguments
group.add_argument('--common', nargs = '*', dest='plFiles', required=False)
group.add_argument('--stats', dest='plFile', required=False)
group.add_argument('--dup', dest='plFileD', required=False)
# parse args
args = parser.parse_args()
if args.plFiles:
# find common tracks
find_common_tracks(args.plFiles)
elif args.plFile:
# plot stats
plot_stats(args.plFile)
elif args.plFileD:
# find duplicate tracks
find_duplicates(args.plFileD)
else:
print("These are not the tracks you are looking for.")
if __name__ == "__main__":
main()最后這個是畫圖的部分,以后有興趣再研究吧

到此這篇關于Python plist文件的讀取方式的文章就介紹到這了,更多相關Python讀取plist文件內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:
相關文章
python3.6+selenium實現(xiàn)操作Frame中的頁面元素
這篇文章主要為大家詳細介紹了python3.6+selenium實現(xiàn)操作Frame中的頁面元素,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-07-07
詳解使用PyInstaller將Pygame庫編寫的小游戲程序打包為exe文件
這篇文章主要介紹了詳解使用PyInstaller將Pygame庫編寫的小游戲程序打包為exe文件,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-08-08
Python實現(xiàn)九宮格式的朋友圈功能內附“馬云”朋友圈
PIL(Python Imaging Library)是一個非常強大的Python庫,但是它支持Python2.X, 在Python3中則使用的是Pillow庫,它是從PIL中fork出來的一個分支。這篇文章主要介紹了用Python搞定九宮格式的朋友圈功能內附“馬云”朋友圈 ,需要的朋友可以參考下2019-05-05
PyTorch中clone()、detach()及相關擴展詳解
這篇文章主要給大家介紹了關于PyTorch中clone()、detach()及相關擴展的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-12-12

