python 读写Plist篇(Plist)
文章目录
一、Plist应用环境
Plist文件主要在macOS和iOS开发环境中使用
。Plist文件通常用于存储应用程序的配置信息、用户偏好设置等。在macOS和iOS开发中,Plist文件具有多种用途,包括但不限于配置文件编辑、用户偏好设置管理、本地化字符串管理和动态数据存储。
二、python环境(plistlib库)
plistlib库是第三方库。它是一个用于处理plist文件的Python库,常用于macOS和iOS开发中。plist文件是一种用于存储数据的文件格式,可以采用XML或二进制格式,可以存储各种类型的数据,如字典、数组、字符串、日期等
python pip 保姆级详解(Mac / Linux / Window)
pip install plistlib
三、plist文件打开方式
1.在这之前,我们需要简单了解一下文件操作Tips,是用with open读写还是open读写csv文件呢/
import plistlib
# [强烈推荐使用] with opend读写:打开操作完后会自动关闭.
with open('config.plist', 'r', newline='') as rf:
data_dict = plistlib.load(rf) # 返回字典格式
# open读写:只会打开,不会自动关闭,需要我们添加个close,而且open模式不能使用plistlib.dump()和plistlib.load()方法.
open('config.plist', 'r', newline='') as rf:
data_list = rf.readlines() # 返回列表格式
rf.close()
四、plist文件示例(config.plist)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CBPasscodes</key>
<dict>
<key>0</key>
<string>1234567890123456789012345678901234567890</string>
</dict>
<key>ChallengePasscodes</key>
<dict>
<key>Pearl</key>
<string>445566778899</string>
</dict>
<key>SigningKeys</key>
<dict>
<key>CalData</key>
<string>AABBCCDDEEFF</string>
</dict>
</dict>
</plist>
五、文件读取
data_dict["ChallengePasscodes"]["Pearl"]
结果:
445566778899
六、文件写入
self.data_dict["ChallengePasscodes"]["Pearl"] = "new_param" # 修改value
self.data_dict["ChallengePasscodes"] = {"new_key" : "new_param"} # 存在则修改key,不存在则创建
七、文件内容删除
del self.data_dict["ChallengePasscodes"]["Pearl"] # 删除指定key
del self.data_dict["ChallengePasscodes"] # 删除整段host
八、代码示例
import plistlib
class RW_Plist:
def __init__(self, path):
self.path = path
with open(self.path, "rb") as rf:
self.data_dict = plistlib.load(rf)
with open(self.path, "rb") as rf:
self.data_list = rf.read()
def Get_All(self):
print(self.data_list)
return self.data_list
def Get_All_dict(self):
print(self.data_dict)
return self.data_dict
def Get_Plist(self, host=None, key=None):
try:
value = self.data_dict[host][key]
print(value)
return value
except KeyError:
if host not in self.data_dict:
print("Ge Success Host is [{}], Host not defined".format(host))
return "Get Success Host is [{}], Host not defined".format(host)
else:
print("Get failure Host is [{}], Key is [{}], Key not defined".format(host, key))
return "Get failure Host is [{}], Key is [{}], Key not defined".format(host, key)
def Set_Plist(self, host=None, key=None, param=None):
try:
try:
self.data_dict[host][key] = param
except:
self.data_dict[host] = {key : param}
with open(self.path, "wb") as wf:
plistlib.dump(self.data_dict, wf)
print("Set Success Host is [{}], Key is [{}], param is [{}]".format(host, key, param))
return "Set Success Host is [{}], Key is [{}], param is [{}]".format(host, key, param)
except Exception as e:
print("Get failure Host is [{}], Key is [{}], param is [{}]".format(host, key, param))
return e
def Remove_Plist(self, host=None, key=None):
try:
if key:
del self.data_dict[host][key] # 删除指定元素
print("Remove Success Host is [{}], key is [{}]".format(host, key))
else:
del self.data_dict[host] # 删除整段数据
print("Remove Success Host is [{}]".format(host))
with open(self.path, "wb") as wf:
plistlib.dump(self.data_dict, wf)
except KeyError:
if host not in self.data_dict:
print("Remove failure Host is [{}], Host not defined".format(host))
return "Remove failure Host is [{}], Host not defined".format(host)
else:
print("Remove failure Host is [{}], Key is [{}], Key not defined".format(host, key))
return "Remove failure Host is [{}], Key is [{}], Key not defined".format(host, key)
if __name__ == "__main__":
import os
code_root_path = os.path.split(os.path.split(os.path.realpath(__file__))[0])[0]
print(code_root_path)
RW_Plist = RW_Plist(code_root_path + "/config.plist")
RW_Plist.Get_All()
# RW_Plist.Get_All_dict()
# RW_Plist.Get_Plist("ChallengePasscodes", "Pearl")
RW_Plist.Set_Plist("CBPasscodes", "0", "111")
RW_Plist.Set_Plist("path3", "stage_list3", "111")
RW_Plist.Remove_Plist("CBPasscodes", "2")
# RW_Plist.Remove_Plist("path3")
如转载此文请联系我征得本人同意,并标注出处及本博主名,谢谢 !