skip to Main Content

I am currently working on a programme within the django environment which operates off a json api provided by a third party. There is an object within that API which I want however the string of information it provides is too much for me.

The data I want is the created_at tag from the twitter api using tweepy. This created_at contains data in the following format:

"created_at": "Mon Aug 27 17:21:03 +0000 2012"

This is all fine however this will return the date AND time whereas I simply want the the time part of the above example i.e. 17:21:03.

Is there any way I can just take this part of the created_at response string and store it in a separate variable?

4

Answers


  1. Try below code.

    my_datetime = response_from_twitter['created_at']
    my_time = my_datetime.split(' ')[3]
    
    # my_time will now contain time part.
    
    Login or Signup to reply.
  2. You could just split the string into a list and take the 4th element:

    time = source['created_at'].split(' ')[3]
    
    Login or Signup to reply.
  3. You can use the dateutil module

    from dateutil import parser
    
    created_at = "Mon Aug 27 17:21:03 +0000 2012"
    created_at = parser.parse(created_at)
    print created_at.time()
    

    Output:

    17:21:03
    
    Login or Signup to reply.
  4. What about a regular expression with re.search():

    >>> import re
    >>> d = {"created_at": "Mon Aug 27 17:21:03 +0000 2012"}
    >>> re.search('d{2}:d{2}:d{2}', d['created_at']).group(0)
    '17:21:03'
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search