0

I am trying to take a list of a list and I would like to split apart the datetime object that is in the first element of each element of the list as shown below.

list = [[u'2014-09-02T23:00:00', 1, 1, u'msdn.microsoft.com', u'Engineering & Technology', 1], [u'2014-09-02T23:00:00', 1, 1, u'qr.ae', u'Uncategorized', 0], [u'2014-09-02T23:00:00', 1, 1, u'accounts.google.com', u'General Communication & Scheduling', 0]]

I saw the previous Stack question about Unicode to Dates (link) and have tried the following code:

date_unicode = str(list[0].split('T'))

date = datetime.strptime(date_unicode, '%Y-%m-%dT%H:%S')

only to receive an error saying the module object has no attribute 'strptime.' If someone would be able to help me not only split the unicode dates into actual date objects for each element of the list I would really appreciate it.

Community
  • 1
  • 1
g.humpkins
  • 331
  • 6
  • 15
  • *Aside*: I hope that your actual program doesn't use `list` as a variable name. One shouldn't use re-use builtin names. – Robᵩ Sep 03 '14 at 21:35
  • Thank you for the help. Also no, it doesn't use `list` as a variable name, but thanks for checking! – g.humpkins Sep 04 '14 at 01:58
  • I think you may be confused on one point. You write, "*the datetime object that is in the first element*", but the sample you provide doesn't have any `datetime` objects. The first item of each list is a `unicode` element. Is your goal to convert the unicode string to a `datetime` object? Or is your goal to convert the unicode string into a pair of strings? – Robᵩ Sep 04 '14 at 14:36

2 Answers2

1
  1. Your list is two-diminsional, so you need an extra index: list[0][0].
  2. Your strptime() format accepts the entire ISO8601 date, so you don't need .split()
  3. strptime() is a method of datetime.datetime
date_unicode = str(list[0][0])
date = datetime.datetime.strptime(date_unicode, '%Y-%m-%dT%H:%M:%S')
print date

Finally, you have several elements of the list, so you'll need a loop:

newlist = [datetime.datetime.strptime(item[0], '%Y-%m-%dT%H:%M:%S')
           for item in list]
print newlist
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
  • Rob, thanks for the help. When I started playing around with the code I realized that the new list contains a list of elements that looks like this: `[datetime.datetime(2014, 9, 2, 23, 0), datetime.datetime(2014, 9, 2, 23, 0), datetime.datetime(2014, 9, 2, 23, 0)]` and I was just wondering if there is a way to adapt this code so that the output of one element of "list" would look something like this: `[2014-09-02, 23:00:00, 1, 1, u'msdn.microsoft.com', u'Engineering & Technology', 1]` rather than with the datetime.datetime tags. – g.humpkins Sep 04 '14 at 03:36
  • From your comment, it looks like you don't want to have `datetime` objects at all. To make a new list, try this: `newlist = [item[0].split('T') + item[1:] for item in list]`. Or, if you want to mutate the existing list: `for item in list: item[0:1] = item[0].split('T')` – Robᵩ Sep 04 '14 at 14:31
  • Yeah the first piece of code you passed along was exactly what I was looking for. When I run it, I get a list of a a list that looks something like this: `[[u'2014-09-02', u'23:00:00', 1, 1, u'msdn.microsoft.com', u'Engineering & Technology', 1], [u'2014-09-02', u'23:00:00', 1, 1, u'qr.ae', u'Uncategorized', 0]]` and I was just wondering if there is a good way to loop through the list to tell python that the dates and times are indeed dates and times (so I could, for example, add or subtract the dates if I wanted) because as I understand it, these are Unicode strings. – g.humpkins Sep 04 '14 at 14:58
  • The code in my answer produces dates and times, which can be manipulated by addition and substraction. The code in my comment produces Unicode strings which cannot. You have to decide whether you want `datetime` objects or strings. – Robᵩ Sep 04 '14 at 15:03
0
from datetime import datetime
print [datetime.strptime(x[0],"%Y-%m-%dT%H:%M:%S") for x in l]
[datetime.datetime(2014, 9, 2, 23, 0), datetime.datetime(2014, 9, 2, 23, 0), datetime.datetime(2014, 9, 2, 23, 0)]

You need to use from datetime import datetime or else datetime.datetime.strptime

Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321