Skip to content

Instantly share code, notes, and snippets.

@harperreed
Created January 30, 2012 04:32
Show Gist options
  • Save harperreed/1702550 to your computer and use it in GitHub Desktop.
Save harperreed/1702550 to your computer and use it in GitHub Desktop.
a def to turn pesterfish object into an associative object
def fix_pesterfish(data):
result = {}
if type(data)==list:
for d in data:
try:
result[d['tag']]=fix_pesterfish(d['children'])
except:
try:
result[d['tag']]=d['text']
except:
try:
if d['attributes']:
if type(result[d['tag']]) != list:
l = []
result[d['tag']] = l
result[d['tag']].append(d['attributes'])
except:
result[d['tag']]=''
else:
try:
result[data['tag']]=fix_pesterfish(data['children'])
except:
try:
result[data['tag']]=data['text']
except:
result[data['tag']]=''
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment