Plistlib.load() codec

import os, plistlib
file_path = os.path.join(os.getcwd(), 'data/JDLoginInfo1.plist')
with open(file_path, 'rb') as fp:
    plist_data = plistlib.loads(fp.read(),fmt=plistlib.FMT_BINARY)
    print(plist_data)

What I get is

{'$version': 100000, '$archiver': 'NSKeyedArchiver', '$top': {'root': UID(1)}, '$objects': ['$null', {...}, b'\x00\x02e"pE\x00@\xa5\xc8\x0b\xcc\x80\xc2\x1f^0\xe4\xab\x8d...v\xb09\xaa\x18\xb8\xd2mJ\xcf', {...}, {...}, 'zGb7qfTD13vvULt-K3WQHA', {...}]}

b"\x00\x02e$\xb1<\x00@\x9f4F\xdd\xb1cUfT\x11u)\x13\x9b+\xed\xd6’+Y=ic\xff,\xb2\x0c6\xb7\xe9h\x08\x16\x83\xe9\x88\x88&\x98\x94\x13\x0c;a\x9a\xc4\xd4\xc6nNO\xa6\x18W\x0b\xa9\x17?oJd\xd8\x93F"
error: ‘utf-8’ codec can’t decode byte 0xb1 in position 4: invalid start byte

And what I want is

<?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>$version</key>
	<integer>100000</integer>
	<key>$archiver</key>
	<string>NSKeyedArchiver</string>
	<key>$top</key>
	<dict>
		<key>root</key>
		<dict>
			<key>CF$UID</key>
			<integer>1</integer>
		</dict>
	</dict>
	<key>$objects</key>
	<array>
		<string>$null</string>
		<dict>
			<key>$class</key>
			<dict>
				<key>CF$UID</key>
				<integer>6</integer>
			</dict>
			<key>a2</key>
			<dict>
				<key>CF$UID</key>
				<integer>2</integer>
			</dict>
			<key>a2ExpiredInterval</key>
			<integer>28512000</integer>
			<key>a2RefreshInterval</key>
			<integer>14256000</integer>
			<key>a2CreateTime</key>
			<dict>
				<key>CF$UID</key>
				<integer>3</integer>
			</dict>
			<key>enPin</key>
			<dict>
				<key>CF$UID</key>
				<integer>5</integer>
			</dict>
			<key>pin</key>
			<dict>
				<key>CF$UID</key>
				<integer>0</integer>
			</dict>
		</dict>
		<data>
		AAJlJLE8AECfNEbdsWNVZlQRdSkTmyvt1icrWT1pY/8ssgw2t+loCBaD6YiI
		JpiUEww7YZrE1MZuTk+mGFcLqRc/b0pk2JNG
		</data>
		<dict>
			<key>NS.time</key>
			<real>718596284.25224495</real>
			<key>$class</key>
			<dict>
				<key>CF$UID</key>
				<integer>4</integer>
			</dict>
		</dict>
		<dict>
			<key>$classname</key>
			<string>NSDate</string>
			<key>$classes</key>
			<array>
				<string>NSDate</string>
				<string>NSObject</string>
			</array>
		</dict>
		<string>o35pxzqeHZUISvt2L6kzHw</string>
		<dict>
			<key>$classname</key>
			<string>WJLoginTGTInfo</string>
			<key>$classes</key>
			<array>
				<string>WJLoginTGTInfo</string>
				<string>NSObject</string>
			</array>
		</dict>
	</array>
</dict>
</plist>

xiangren deng said:

import os, plistlib
file_path = os.path.join(os.getcwd(), 'data/JDLoginInfo1.plist')
with open(file_path, 'rb') as fp:
   plist_data = plistlib.loads(fp.read(),fmt=plistlib.FMT_BINARY)
   print(plist_data)

What I get is

{'$version': 100000, '$archiver': 'NSKeyedArchiver', '$top': {'root': UID(1)}, '$objects': ['$null', {...}, b'\x00\x02e"pE\x00@\xa5\xc8\x0b\xcc\x80\xc2\x1f^0\xe4\xab\x8d...v\xb09\xaa\x18\xb8\xd2mJ\xcf', {...}, {...}, 'zGb7qfTD13vvULt-K3WQHA', {...}]}

b"\x00\x02e$\xb1<\x00@\x9f4F\xdd\xb1cUfT\x11u)\x13\x9b+\xed\xd6’+Y=ic\xff,\xb2\x0c6\xb7\xe9h\x08\x16\x83\xe9\x88\x88&\x98\x94\x13\x0c;a\x9a\xc4\xd4\xc6nNO\xa6\x18W\x0b\xa9\x17?oJd\xd8\x93F"
error: ‘utf-8’ codec can’t decode byte 0xb1 in position 4: invalid start byte

Can you show us the full traceback? Which line from the above produces
it. Or is the code above not complete?

It looks like you get a dict back, which seems sane. So what’s
generating the decode error?

And what I want is

You’ll want to then use dump to write things out in XML format. See
the docs:

There is a macOS command to convert binary plist into text plist plutil.
For example:

plutil -convert xml1 app.plist -o /dev/tty

See man plutil for its documentation,
I use -o /dev/tty in the example as a way yo print out the plist.

You can use -o - to write to standard output, too. - Cameron

b'\x00\x02e$\xb1<\x00@\x9f4F\xdd\xb1cUfT\x11u)\x13\x9b+\xed\xd6'+Y=ic\xff,\xb2\x0c6\xb7\xe9h\x08\x16\x83\xe9\x88\x88&\x98\x94\x13\x0c;a\x9a\xc4\xd4\xc6nNO\xa6\x18W\x0b\xa9\x17?oJd\xd8\x93F'

Please tell me how to convert the read b data into str

converted to:

AAJlJLE8AECfNEbdsWNVZlQRdSkTmyvt1icrWT1pY/8ssgw2t+loCBaD6YiI
		JpiUEww7YZrE1MZuTk+mGFcLqRc/b0pk2JNG

I do not understand what your question is?
You seemed to want to know how to convert from binary format to xml format.
We have shown how to do that.

Why do you want to know how to convert the byte string into the xml format used by plist for its element?

Don’t you want your code to process the bytes not the apple plist encoded bytes.