d['children'] = [] for k, v in d.iteritems(): if isinstance(v, OrderedDict): if k == 'children': d[k] = [v] reformat_dictionary(v) elif isinstance(v, list): for e in v: reformat_dictionary(e) else: if k == 'longitude' or k == 'latitude': d[k] = float(v) if k == 'name': count[0] += 1 if count[0] % 100 == 0: print "Formatting city {0}".format(count[0]) d = DataUtility() print 'Reading the XML file...' xml_content = d.read_xml_file('cities.xml') reformat_dictionary(xml_content) xml_content = xml_content['root'] cities = {'meta': {'dataType': 'cities', 'temporal': False}, 'root': xml_content} print 'Writing the JSON file...' d.write_json_file(cities, 'cities.json', True)
from DataUtility import DataUtility d = DataUtility() # Read an example CSV file csv_file = d.read_csv_file('example-data/test.csv') print csv_file # Access a specific row and key print csv_file[0]['author'] # Read an example XML file xml_file = d.read_xml_file('example-data/test.xml') print xml_file # Access a specific entry from the XML tree print xml_file['cities']['stadt']['stadt'][1]['name'] # Read an example JSON file json_file = d.read_json_file('example-data/test.json') print json_file # Access a specific entry from the JSON tree print json_file['globe']['radius'] # Write one of the dictionaries to a new JSON file d.write_json_file(csv_file, 'example-data/output.json')