AnsweredAssumed Answered

csv.DictWriter Help convert epoch dates

Question asked by jamesfreddyc on Feb 22, 2018
Latest reply on Feb 22, 2018 by jamesfreddyc

I'm looking for assistance to writing a dictionary to a .csv file.  First, set a "gdata2" variable:

taburl =  hostedFeatureService + '/{}/query/0'
where = '1=1'
query = "?where={}&outFields={}&returnGeometry=true&f=json&token={}".format(where, fieldsM, token)
queryfsURL = taburl + query
greq = urllib2.Request(queryfsURL)
gresp = urllib2.urlopen(greq)
gdata = json.load(gresp)
gdata2 = gdata['features']
print gdata2


This codeblock gives me the gdata2 variable result as:

[{u'geometry': {u'y': 26.67525376665167, u'x': -80.09297701660482}, u'attributes': {u'Ingress1Arrive': 1519133580000L, u'SurveyDate': 1519102800000L, u'globalid': u'fdd28a97-9a28-400d-8284-5ce91868f26e', u'objectid': 3}}, {u'geometry': {u'y': 26.67525546250736, u'x': -80.0929301851895}, u'attributes': {u'Ingress1Arrive': 1519136280000L, u'SurveyDate': 1519102800000L, u'globalid': u'cc83f1b4-6335-4659-8463-9d6d50d82bd7', u'objectid': 4}}, {u'geometry': {u'y': 26.675340745164966, u'x': -80.09289801628141}, u'attributes': {u'Ingress1Arrive': 1519227780000L, u'SurveyDate': 1519189200000L, u'globalid': u'99ed6b91-6823-4702-868a-2bb4dbd32cbf', u'objectid': 5}}]


From this I am attempting to write this result to a .csv file.  It actually works great however I am having an issue with date values (Ingress1Arrive and SurveyDate) as they are epoch formatted values because this source is a hosted feature service.


The DictWriter proc:


with open('C:temp\{}.csv'.format(sheetname), 'wb') as outf:
           dw = csv.DictWriter(outf,  delimiter=",", quotechar="|", fieldnames=['objectid','globalid','SurveyDate','Ingress1Arrive'])
                headers = {}
           for n in dw.fieldnames:
               headers[n] = n
           for row in gdata2:


Any recommendations on how/where to perform a conversion prior to writing the final .csv output so that the date format is something that makes sense?