-
Notifications
You must be signed in to change notification settings - Fork 1
/
parsing json.py
39 lines (33 loc) · 1 KB
/
parsing json.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import os, sys
from os import path
import more_itertools
from more_itertools import unique_everseen
from operator import itemgetter
from pprint import pprint
import json
import xml.etree.ElementTree as ET
import xmltodict
import csv
# Load json into dictionary
json_object = json.load(open('/Users/mark.jacobson/Desktop/sample json.json'))
# Pretty print json
pprint(json_object)
# Loop through json structure
for k,v in json_object.iteritems():
print "the key is %s" % (k)
print "and its value is %s" % (v)
for i,j in v.iteritems():
print "the next key is %s" % (i)
print "and its value is %s" % (j)
# pprint(i)
# Load xml
with open('/Users/mark.jacobson/Desktop/sample xml.xml') as fd:
xml_object = xmltodict.parse(fd.read())
print xml_object['widget']
print xml_object['widget']['debug']
print xml_object['widget']['window']
# Load data from csv
with open('/Users/mark.jacobson/Desktop/foo.csv','rb') as f:
reader = csv.reader(f)
for row in reader:
print row