0
content_dict = [(u'Bowe Bergdahl', [u'Sat, 31 May 2014 16:03:32 EDT']), (u"U.S. 'hypocrisy' in cybertheft charge", [u'Fri, 23 May 2014 02:30:44 EDT'])

What is wrong with this lambda function:

content2_dict =  sorted(content_dict, key=lambda x: datetime.strptime(x[0],'%a, %d %b %H:%M:%S %z'))

I get the following error:

Traceback (most recent call last):
  File "/Users/amirnakhostin/Documents/Computer Science /Python/test.py", line 17, in <module>
    content2_dict =  sorted(content_dict, key=lambda x: datetime.strptime(x[1],'%a, %d %b %H:%M:%S'))
  File "/Users/amirnakhostin/Documents/Computer Science /Python/test.py", line 17, in <lambda>
    content2_dict =  sorted(content_dict, key=lambda x: datetime.strptime(x[1],'%a, %d %b %H:%M:%S'))
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_strptime.py", line 325, in _strptime
    (data_string, format))
ValueError: time data 'o' does not match format '%a, %d %b %H:%M:%S'
BenMorel
  • 30,280
  • 40
  • 163
  • 285

4 Answers4

1

I also had a problem with a timezone, had to remove it.

import datetime

content_dict = [(u'Bowe Bergdahl', [u'Sat, 31 May 2014 16:03:32']), (u"U.S. 'hypocrisy' in cybertheft charge", [u'Fri, 23 May 2014 02:30:44'])]

content2_dict =  sorted(content_dict, key=lambda x: datetime.datetime.strptime(x[1][0],'%a, %d %b %Y %H:%M:%S'))
print content2_dict
yoK0
  • 315
  • 1
  • 3
  • 16
0

There is a missing %Y between %b and %H:%M:%S.

To access a time string in your content_dict, x[1][0] should be used instead of x[0].

Here lies a workaround solution, since python (or datetime module) has some problem handling time zones (HKT can be parsed while EDT can't which confuses me) and assuming that all the timezones are the same, I just simply stripped all the time zones away.

content2_dict = sorted(content_dict, key=lambda x: datetime.strptime(x[1][0][:-4], '%a, %d %b %Y %H:%M:%S'))
Saren Arterius
  • 1,212
  • 1
  • 10
  • 15
0

There are a couple problems, you are indexing the wrong thing in your lambda, you forgot a year, %H:%M:%S can be written as %X, and EDT, unfortunately, can't be parsed:

from datetime import datetime
content_dict = [(u'Bowe Bergdahl', [u'Sat, 31 May 2014 16:03:32']), (u"U.S. 'hypocrisy' in cybertheft charge", [u'Fri, 23 May 2014 02:30:44'])]
content_dict_ = sorted(content_dict, key=lambda x: datetime.strptime(x[1][0],'%a, %d %B %Y %X'))
Dair
  • 14,712
  • 7
  • 54
  • 91
0

Several problems with your code. You improperly formatted strptime by omitting the year %Y. You also enclosed the date in a list which caused other problems. You were trying to use x[0] when your date was enclosed in x[1]. Here is a working example of (almost all) of your code (note that the time zone has been removed. See below for why).

>>> content = [(u'Bowe Bergdahl',u'Sat, 31 May 2014 16:03:32'), (u"U.S. 'hypocrisy' in cybertheft charge", u'Fri, 23 May 2014 02:30:44')]
>>> content2 = sorted(content, key=lambda x:datetime.datetime.strptime(x[1], '%a, %d %B %Y %H:%M:%S'))
>>> content2
[(u"U.S. 'hypocrisy' in cybertheft charge", u'Fri, 23 May 2014 02:30:44'), (u'Bowe Bergdahl', u'Sat, 31 May 2014 16:03:32')]

However we get from this question that strptime has some issues with time zones. To fix that issue we use the dateutil package

>>> from dateutil import parser
>>> parser.parse(u'Sat, 31 May 2014 16:03:32 -0400')
datetime.datetime(2014, 5, 31, 16, 3, 32, tzinfo=tzoffset(None, -14400))

Note that -0400 is EDT relative to GMT.

To use this to sort your list do something like the following

>>> from dateutil import parser
>>> unformatted_content = [(u'Bowe Bergdahl', u'Sat, 31 May 2014 16:03:32 -0400'), (u"U.S. 'hypocrisy' in cybertheft charge", u'Fri, 23 May 2014 02:30:44 -0400')]
>>> real_content = [(item[0], parser.parse(item[1])) for item in unformatted_content]
>>> real_content
[(u'Bowe Bergdahl', datetime.datetime(2014, 5, 31, 16, 3, 32, tzinfo=tzoffset(None, -14400))), (u"U.S. 'hypocrisy' in cybertheft charge", datetime.datetime(2014, 5, 23, 2, 30, 44, tzinfo=tzoffset(None, -14400)))]
>>> content2 = sorted(real_content, key=lambda x:x[1])
>>> content2
[(u"U.S. 'hypocrisy' in cybertheft charge", datetime.datetime(2014, 5, 23, 2, 30, 44, tzinfo=tzoffset(None, -14400))), (u'Bowe Bergdahl', datetime.datetime(2014, 5, 31, 16, 3, 32, tzinfo=tzoffset(None, -14400)))]
Community
  • 1
  • 1
Dan Oberlam
  • 2,267
  • 8
  • 30
  • 48
  • how do I use the dateutil in a for loop to parse the time stamp? – AmirNakhostin Jun 01 '14 at 08:18
  • using the `parser.parse()` returns a datetime object. Just sort by the returned object – Dan Oberlam Jun 01 '14 at 14:35
  • @AmirNakhostin I have edited my answer to answer your comment. – Dan Oberlam Jun 02 '14 at 01:00
  • This only works if I have 3 values to parse but the content[] is a list of 200 articles. so is it use a parser in a loop to parse out the time zone and create a new tuple with title and new time? – AmirNakhostin Jun 02 '14 at 04:39
  • thank you I thought i was it but I still get an error. I think it is the format of the original time stamp that causing the issue. – AmirNakhostin Jun 02 '14 at 05:37
  • in parse res, skipped_tokens = self._parse(timestr, **kwargs) TypeError: 'NoneType' object is not iterable – AmirNakhostin Jun 02 '14 at 05:48
  • This is the whole codes: from datetime import datetime from dateutil import parser import datetime import feedparser content = {} content2= {} content3= {} source = ("http://news.yahoo.com/rss/", "http://rss.cnn.com/rss/edition.rss" ) for i in source: content = feedparser.parse(i) for feed in content.entries: content2[feed.title] = [feed.published] content3 = [(item[0], parser.parse(item[1])) for item in content2] print content3 – AmirNakhostin Jun 02 '14 at 05:48
  • @AmirNakhostin I think that might call for you to ask a new question, that seems like a new issue. If any answer(s) here have helped you consider upvoting or accepting them. – Dan Oberlam Jun 02 '14 at 05:55
  • absolutely as soon as I can. This was my first question. I am hella newbee in whole programming world. lol Let me ask 15 questions an I sure will vote you up. Thank you for you help – AmirNakhostin Jun 02 '14 at 05:59
  • @AmirNakhostin You don't need any rep to [accept](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) an answer. It'll actually give you +2 rep yourself. – Dan Oberlam Jun 03 '14 at 02:28