@@ -32,26 +32,23 @@ def transformData(primaryKey=""):
3232 for j in i .keys ():
3333 if j not in header :
3434 header .append (j )
35- with open (outputFileName + ".csv" , 'wb' ) as output_file :
36- fieldnames = list (header )
37- writer = csv .DictWriter (output_file , fieldnames , delimiter = ',' , quotechar = '"' )
38- result .append (header )
39- for x in data :
40- row_value = {}
41- for y in x .keys ():
42- yValue = x .get (y )
43- if type (yValue ) == int or type (yValue ) == bool or type (yValue ) == float or type (yValue ) == list :
44- row_value [y ] = str (yValue ).encode ('utf8' )
45- elif type (yValue ) == unicode :
46- row_value [y ] = yValue .encode ('utf-8' )
47- result .append (row_value .values ())
48- return fieldnames ,result
35+ result .append (header )
36+ for x in data :
37+ row_value = {}
38+ for y in x .keys ():
39+ yValue = x .get (y )
40+ if type (yValue ) == int or type (yValue ) == bool or type (yValue ) == float or type (yValue ) == list :
41+ row_value [y ] = str (yValue ).encode ('utf8' )
42+ elif type (yValue ) == unicode :
43+ row_value [y ] = yValue .encode ('utf-8' )
44+ result .append (row_value .values ())
45+ return result
4946
5047if __name__ == '__main__' :
5148 url = sys .argv [1 ]
5249 data_load = GetUrl (url ) # load data from the API
5350 jsonFile (data_load ) # write to json file
54- filednames , trans_data = transformData () # transform json to csv
51+ trans_data = transformData () # transform json to csv
5552 with open ('jsonToCsvResult.csv' ,'wb' ) as f :
5653 writer = csv .writer (f )
5754 writer .writerows (trans_data )
0 commit comments