Skip to content

Instantly share code, notes, and snippets.

@sethherr
Created October 21, 2014 01:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sethherr/1fa7660e6f5b16fe2b6d to your computer and use it in GitHub Desktop.
Save sethherr/1fa7660e6f5b16fe2b6d to your computer and use it in GitHub Desktop.
Parse some JSON for the CWW
# This relies on chicago-classifications.json being in the
# same folder as this parse_json.py file
#
# It will then make a .tsv from the JSON stream
# Kickass, Mr. Fidino and @sethherr
import json
cww_json = open( "chicago-classifications.json", "r" )
export = open('cww_tab.tsv', 'w')
export.write("species\tcount\tbehavior\tbabies\tid\n")
export.close()
for line in cww_json:
photo_data = json.loads(line)
if "species" in photo_data["annotations"][0]:
tsv_line = photo_data["annotations"][0]["species"]
else:
tsv_line = "nothing"
tsv_line += "\t"
if "count" in photo_data["annotations"][0]:
tsv_line += photo_data["annotations"][0]["count"]
else:
tsv_line += "NA"
tsv_line += "\t"
if "behavior" in photo_data["annotations"][0]:
tsv_line += ', '.join(photo_data["annotations"][0]["behavior"])
else:
tsv_line += "NA"
tsv_line += "\t"
if "babies" in photo_data["annotations"][0]:
tsv_line += photo_data["annotations"][0]["babies"]
else:
tsv_line += "NA"
tsv_line += "\t"
tsv_line += photo_data['_id']['$oid']
print tsv_line
tsv_line += "\n"
with open('cww_tab.tsv', 'a') as file:
file.write(tsv_line)
cww_json.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment