Skip to content

Commit

Permalink
pythongh-101992: update plistlib examples to be runnable (pythonGH-10…
Browse files Browse the repository at this point in the history
…1994)

* pythongh-101992: update plistlib examples to be runnable

* Update Doc/library/plistlib.rst

---------

(cherry picked from commit a1723ca)

Co-authored-by: Dustin Rodrigues <dust.rod@gmail.com>
Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
  • Loading branch information
2 people authored and miss-islington committed Feb 17, 2023
1 parent f4f5dd5 commit 2971a73
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions Doc/library/plistlib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ Examples

Generating a plist::

import datetime
import plistlib

pl = dict(
aString = "Doodah",
aList = ["A", "B", 12, 32.1, [1, 2, 3]],
Expand All @@ -172,13 +175,19 @@ Generating a plist::
),
someData = b"<binary gunk>",
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:
dump(pl, fp)
print(plistlib.dumps(pl).decode())

Parsing a plist::

with open(fileName, 'rb') as fp:
pl = load(fp)
print(pl["aKey"])
import plistlib

plist = b"""<plist version="1.0">
<dict>
<key>foo</key>
<string>bar</string>
</dict>
</plist>"""
pl = plistlib.loads(plist)
print(pl["foo"])

0 comments on commit 2971a73

Please sign in to comment.