本文整理汇总了Python中plistlib.py方法的典型用法代码示例。如果您正苦于以下问题:Python plistlib.py方法的具体用法?Python plistlib.py怎么用?Python plistlib.py使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类plistlib
的用法示例。
在下文中一共展示了plistlib.py方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: dumps
# 需要导入模块: import plistlib [as 别名]
# 或者: from plistlib import py [as 别名]
def dumps(value, fmt=FMT_XML, skipkeys=False, sort_keys=True):
if _check_py3():
return plistlib.dumps(value, fmt=fmt, skipkeys=skipkeys, sort_keys=sort_keys).decode("utf-8")
else:
# We avoid using writePlistToString() as that uses
# cStringIO and fails when Unicode strings are detected
f = StringIO()
dump(value, f, fmt=fmt, skipkeys=skipkeys, sort_keys=sort_keys)
return f.getvalue()
### ###
# Binary Plist Stuff For Py2 #
### ###
# From the python 3 plistlib.py source: https://github.com/python/cpython/blob/3.7/Lib/plistlib.py
# Tweaked to function on Python 2
示例2: load
# 需要导入模块: import plistlib [as 别名]
# 或者: from plistlib import py [as 别名]
def load(fp, fmt=None, use_builtin_types=None, dict_type=dict):
if _check_py3():
use_builtin_types = True if use_builtin_types == None else use_builtin_types
# We need to monkey patch this to allow for hex integers - code taken/modified from
# https://github.com/python/cpython/blob/3.8/Lib/plistlib.py
if fmt is None:
header = fp.read(32)
fp.seek(0)
for info in plistlib._FORMATS.values():
if info['detect'](header):
P = info['parser']
break
else:
raise plistlib.InvalidFileException()
else:
P = plistlib._FORMATS[fmt]['parser']
p = P(use_builtin_types=use_builtin_types, dict_type=dict_type)
if isinstance(p,plistlib._PlistParser):
# Monkey patch!
def end_integer():
d = p.get_data()
p.add_object(int(d,16) if d.lower().startswith("0x") else int(d))
p.end_integer = end_integer
return p.parse(fp)
elif not _is_binary(fp):
# Is not binary - assume a string - and try to load
# We avoid using readPlistFromString() as that uses
# cStringIO and fails when Unicode strings are detected
# Don't subclass - keep the parser local
from xml.parsers.expat import ParserCreate
# Create a new PlistParser object - then we need to set up
# the values and parse.
p = plistlib.PlistParser()
# We also need to monkey patch this to allow for other dict_types
def begin_dict(attrs):
d = dict_type()
p.addObject(d)
p.stack.append(d)
def end_integer():
d = p.getData()
p.addObject(int(d,16) if d.lower().startswith("0x") else int(d))
p.begin_dict = begin_dict
p.end_integer = end_integer
parser = ParserCreate()
parser.StartElementHandler = p.handleBeginElement
parser.EndElementHandler = p.handleEndElement
parser.CharacterDataHandler = p.handleData
if isinstance(fp, unicode):
# Encode unicode -> string; use utf-8 for safety
fp = fp.encode("utf-8")
if isinstance(fp, basestring):
# It's a string - let's wrap it up
fp = StringIO(fp)
# Parse it
parser.ParseFile(fp)
return p.root
else:
use_builtin_types = False if use_builtin_types == None else use_builtin_types
p = _BinaryPlistParser(use_builtin_types=use_builtin_types, dict_type=dict_type)
return p.parse(fp)