Hello Community,
I have transformed my bookmarks plist file to an XML file. I try to write a script where I am able to read the XML file, getting the URL information and generate a SQLite3 file with it.
I checked
the Python 3 documentation about plistlib. I tried the following
import plistlib
fileName = "Bookmarks_example.xml"
print ("File: ", fileName)
print (" ")
with open(fileName, 'rb') as fp:
pl = plistlib.load(fp)
print(pl["Children"])
When I try something else except Children I got a key error message
Error:
python3 helper_plist-xml_2.py
File: Bookmarks_example.xml
Traceback (most recent call last):
File "/home/wchris/Python/helper_plist-xml_2.py", line 9, in <module>
print(pl["string"])
KeyError: 'string'
Is it only possible to access key values?
How can I get the values from <string> so I can get the URL information?
I have add an example XML file
Best regards
--Christian
(May-26-2021, 11:03 AM)Tecuma Wrote: [ -> ] pl = plistlib.load(fp)
What happens if you do:
pl = plistlib.load(fp, fmt="FMT_XML")
?
When I add this code with fmt I got
Error:
python3 helper_plist-xml_2.py
File: Bookmarks_example.xml
Traceback (most recent call last):
File "/home/wchris/Python/helper_plist-xml_2.py", line 8, in <module>
pl = plistlib.load(fp, fmt="FMT_XML")
File "/usr/lib/python3.9/plistlib.py", line 872, in load
P = _FORMATS[fmt]['parser']
KeyError: 'FMT_XML'
You must understand the structure. Simple test:
import plistlib as pl
with open('Bookmarks_example.xml', 'rb') as f:
data = pl.load(f)
for i, (key, value) in enumerate(data.items(), start=1):
print(f'{i}. Key: {key}, value type: {type(value)}')
Which will display:
Output:
1. Key: Children, value type: <class 'list'>
So there is one key which has value of list type. Lets look what types does this list contains:
for i, item in enumerate(data['Children'], start=1):
print(f'{i}.{type(item)}')
Output:
1.<class 'dict'>
2.<class 'dict'>
So there are two dictionaries in this list. What keys does these dictionaries have:
for i in range(2):
print(f'{", ".join(data["Children"][i].keys())}')
Output:
Title, WebBookmarkIdentifier, WebBookmarkType, WebBookmarkUUID
Children, Sync, Title, WebBookmarkFileVersion, WebBookmarkType, WebBookmarkUUID
...and so on.
Of course, one can print data outright and try to understand the structure :-).
Wow
Thank you very much.
Quote:Of course, one can print data outright and try to understand the structure :-).
No. I like your way of analyze such a XML structure.
Is this more a question of experience with Python or "just" a lack of knowledge or both?
I have about 4 years "experience" programming Python. I had no seminars or something similar. Only figure it out myself (try /error and searching in the internet).
Quote:..and so on
I have to be honest. I do not understand it completly. Is there a documentation about plist and explanations how to handle it. I am willing to learn and read and test but I need a documentation with explanations.
There are two options to get help:
- from interactive interpreter
- from
plistlib documentation
On interactive interpreter:
>>> import plistlib as pl
>>> pl. # press two times TAB
pl.BytesIO( pl.PlistFormat( pl.enum pl.readPlistFromBytes(
pl.Data( pl.binascii pl.itertools pl.struct
pl.FMT_BINARY pl.codecs pl.load( pl.warn(
pl.FMT_XML pl.contextlib pl.loads( pl.writePlist(
pl.InvalidFileException( pl.datetime pl.os pl.writePlistToBytes(
pl.PLISTHEADER pl.dump( pl.re
pl.ParserCreate( pl.dumps( pl.readPlist(
>>> help(pl.readPlist) # help on specific item
Help on function readPlist in module plistlib:
readPlist(pathOrFile)
Read a .plist from a path or file. pathOrFile should either
be a file name, or a readable binary file object.
This function is deprecated, use load instead.
# press Q to quit help
Ok. Thank you. I guess I will use a mix of interpreter and the documentation.
One question to #4.
Quote:So there are two dictionaries in this list. What keys does these dictionaries have:
Do I understand it correctly that I can use Python dictionary mechanism to continue getting the information I seek?