Parse some JSON for the CWW
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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