mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
gh-101992: update plistlib examples to be runnable (#101994)
* gh-101992: update plistlib examples to be runnable * Update Doc/library/plistlib.rst --------- Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
This commit is contained in:
parent
f482ade4c7
commit
a1723caabf
1 changed files with 15 additions and 6 deletions
|
@ -159,6 +159,9 @@ Examples
|
||||||
|
|
||||||
Generating a plist::
|
Generating a plist::
|
||||||
|
|
||||||
|
import datetime
|
||||||
|
import plistlib
|
||||||
|
|
||||||
pl = dict(
|
pl = dict(
|
||||||
aString = "Doodah",
|
aString = "Doodah",
|
||||||
aList = ["A", "B", 12, 32.1, [1, 2, 3]],
|
aList = ["A", "B", 12, 32.1, [1, 2, 3]],
|
||||||
|
@ -172,13 +175,19 @@ Generating a plist::
|
||||||
),
|
),
|
||||||
someData = b"<binary gunk>",
|
someData = b"<binary gunk>",
|
||||||
someMoreData = b"<lots of binary gunk>" * 10,
|
someMoreData = b"<lots of binary gunk>" * 10,
|
||||||
aDate = datetime.datetime.fromtimestamp(time.mktime(time.gmtime())),
|
aDate = datetime.datetime.now()
|
||||||
)
|
)
|
||||||
with open(fileName, 'wb') as fp:
|
print(plistlib.dumps(pl).decode())
|
||||||
dump(pl, fp)
|
|
||||||
|
|
||||||
Parsing a plist::
|
Parsing a plist::
|
||||||
|
|
||||||
with open(fileName, 'rb') as fp:
|
import plistlib
|
||||||
pl = load(fp)
|
|
||||||
print(pl["aKey"])
|
plist = b"""<plist version="1.0">
|
||||||
|
<dict>
|
||||||
|
<key>foo</key>
|
||||||
|
<string>bar</string>
|
||||||
|
</dict>
|
||||||
|
</plist>"""
|
||||||
|
pl = plistlib.loads(plist)
|
||||||
|
print(pl["foo"])
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue