I'm trying to convert a datetime, but I can't figure out how to get my string to be an integer. I've tried adding in [0] after (entry["created_at"] but not luck
Code:
$
for item in data:
`$` convertedTweet = {}
for entry in item:
convertedTweet["created_at"] = item["created_at"]
fmt = '%Y-%m-%dT%H:%M:%SZ'
temp = datetime.strptime(entry['created_at'],'%a %b %d %H:%M:%S +0000 %Y').replace(tzinfo=pytz.UTC)
print temp.strftime(fmt)
Error:
temp = datetime.strptime(entry['created_at'],'%a %b %d %H:%M:%S +0000 %Y').replace(tzinfo=pytz.UTC)
TypeError: string indices must be integers
Looks like item
is a dictionary ( as you do this - item["created_at"]
, and it does not error out for you ) .
If so, when you loop through the dictionary as -
for entry in item:
entry
represents the keys in the dictionary, which most probably are strings. So when you try to do - entry['created_at']
- it errors out as you are trying to use string indices for strings , which is not possible.
It does not look like you need loop at all, you can directly access - item["created_at"]
- to get the datetime. Example -
convertedTweet["created_at"] = item["created_at"]
temp = datetime.strptime(item["created_at"],'%a %b %d %H:%M:%S +0000 %Y').replace(tzinfo=pytz.UTC)
print temp.strftime('%Y-%m-%dT%H:%M:%SZ')
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments