Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
"'{\"animal_list\": [{\"type\": \"mammal\", \"description\": \"Tall, with brown spots, lives in Savanna\", \"name\": \"Giraffa camelopardalis\"},{\"type\": \"mammal\", \"description\": \"Big, grey, with big ears, smart\", \"name\": \"Loxodonta africana\"},{\"type\": \"reptile\", \"description\": \"Green, changes color, lives in \"East Africa\"\", \"name\": \"Trioceros jacksonii\"}]}'"
import json
newfile = open('animals_editted.json', 'w')
with open('animals.json', 'r') as jsonf:
for line in jsonf: # there will be only one line
newline = line.replace('},{', '},\n{')
newline = newline.strip('\"').strip('\'')
newline = newline.replace('\\\"', '\"')
newfile.write(newline)
newfile.close()
# See if this gives errors
#with open('animals_editted.json', 'r') as nf:
# json_object = json.load(nf)
#print len(json_object['animal_list'])
newfile = open('animals_cleaned.json', 'w')
with open('animals_editted.json', 'r') as jsonf:
for line in jsonf:
start, end = line.split('\"description\": \"')
description_value, end = end.split('", "')
description_value = description_value.replace('"', '\\"')
description = '\"description\": \"' + description_value + '", "'
newline = start + description + end
newfile.write(newline)
newfile.close()
newfile = open('animals_cleaned.json', 'w')
with open('animals_editted.json', 'r') as jsonf:
for line in jsonf:
start, end = line.split('\"description\": \"')
descr_val, end = end.split('", "')
descr_val = descr_val.replace('"', '\\"') # escape the quotes
descr = '\"description\": \"' + descr_val + '", "'
newline = start + descr + end
newfile.write(newline)
newfile.close()
with open('animals_cleaned.json', 'r') as nf:
json_object = json.load(nf)
print len(json_object['animal_list'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment