|
| 1 | +#!/usr/bin/env python |
| 2 | +#coding=utf-8 |
| 3 | +import sys |
| 4 | +print sys.getdefaultencoding() |
| 5 | +reload(sys) |
| 6 | +sys.setdefaultencoding('utf-8') |
| 7 | +import json |
| 8 | +import csv |
| 9 | +import types |
| 10 | +import urllib2 |
| 11 | + |
| 12 | +def registerUrl(url): |
| 13 | + try: |
| 14 | + data = urllib2.urlopen(url).read() |
| 15 | + return data |
| 16 | + except Exception,e: |
| 17 | + print e |
| 18 | + |
| 19 | + |
| 20 | +def jsonFile(fileData): |
| 21 | + file = open("output.json","w") |
| 22 | + file.write(fileData) |
| 23 | + file.close() |
| 24 | + |
| 25 | +def transformData(primaryKey=""): |
| 26 | + input = open("output.json") |
| 27 | + data = json.load(input) |
| 28 | + input.close() |
| 29 | + header = [] |
| 30 | + result = [] |
| 31 | + outputFileName = 'jsonToCsvResult' |
| 32 | + for i in data: |
| 33 | + for j in i.keys(): |
| 34 | + if j not in header: |
| 35 | + header.append(j) |
| 36 | + with open(outputFileName+".csv", 'wb') as output_file: |
| 37 | + fieldnames = list(header) |
| 38 | + writer = csv.DictWriter(output_file, fieldnames, delimiter=',', quotechar='"') |
| 39 | + result.append(header) |
| 40 | + for x in data: |
| 41 | + # print x |
| 42 | + row_value = {} |
| 43 | + for y in x.keys(): |
| 44 | + yValue = x.get(y) |
| 45 | + # print yValue |
| 46 | + if type(yValue) == int or type(yValue) == bool or type(yValue) == float or type(yValue) == list: |
| 47 | + row_value[y] = str(yValue).encode('utf8') |
| 48 | + elif type(yValue) == unicode: |
| 49 | + row_value[y] = yValue.encode('utf-8') |
| 50 | + # print row_value.values() |
| 51 | + result.append(row_value.values()) |
| 52 | + return fieldnames,result |
| 53 | + |
| 54 | +if __name__ == '__main__': |
| 55 | + url = sys.argv[1] |
| 56 | + data_load = registerUrl(url) # load data from the API |
| 57 | + jsonFile(data_load) # write to json file |
| 58 | + filednames,trans_data = transformData() # transform json to csv |
| 59 | + with open('jsonToCsvResult.csv','wb') as f: |
| 60 | + writer = csv.writer(f) |
| 61 | + # writer.writerow(filednames) |
| 62 | + writer.writerows(trans_data) |
| 63 | + f.close() |
| 64 | + |
0 commit comments