Question

I've data like this.

startDateTime: {'timeZoneID': 'America/New_York', 'date': {'year': '2014', 'day': '29', 'month': '1'}, 'second': '0', 'hour': '12', 'minute': '0'}

This is just a representation for 1 attribute. Like this i've 5 other attributes. LastModified, created etc.

I wanted to derive this as ISO Date format yyyy-mm-dd hh:mi:ss. is this the right way for doing this?

def parse_date(datecol):
  x=datecol;
  y=str(x.get('date').get('year'))+'-'+str(x.get('date').get('month')).zfill(2)+'-'+str(x.get('date').get('day')).zfill(2)+' '+str(x.get('hour')).zfill(2)+':'+str(x.get('minute')).zfill(2)+':'+str(x.get('second')).zfill(2)
  print y;
  return;
Était-ce utile?

La solution

That works, but I'd say it's cleaner to use the string formatting operator here:

def parse_date(c):
    d = c["date"]
    print "%04d-%02d-%02d %02d:%02d:%02d" % tuple(map(str, (d["year"], d["month"], d["day"], c["hour"], c["minute"], c["second"])))

Alternatively, you can use the time module to convert your fields into a Python time value, and then format that using strftime. Remember the time zone, though.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top