Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Be Python 3.x-compatible and respect xpath index access methods #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions json_lxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"""convert between python value and lxml.etree so that lxml's power can be applied to json data.
In particular, xpath queries can be run against json.
"""
import sys
from lxml import etree

type_map = dict(
Expand All @@ -25,7 +26,7 @@ def element(k, v):
if isinstance(v, dict):
for ck, cv in v.items():
node.append(element(ck, cv))
elif isinstance(v, unicode):
elif sys.version_info < (3,0,0) and isinstance(v, unicode):
node.set('type', type(v).__name__)
node.text=v.encode('utf8')
elif isinstance(v, (int, float, bool, str, NoneType)): # scalar
Expand All @@ -34,7 +35,7 @@ def element(k, v):
elif isinstance(v, list):
node.set('type', type(v).__name__) # list xx this could be done across the board.
for i, cv in enumerate(v):
node.append(element("_list_element_%d" % i, cv))
node.append(element("item", cv))
else:
assert False

Expand Down