Created
July 16, 2021 13:09
-
-
Save rg089/022820f3fd1ec82bcab3b749d7195fe3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import datetime | |
def convert_to_datetime(articles): | |
""" | |
converts the field 'time' in each article from a string to python datetime objects | |
@params | |
articles: a list of dictionaries obtained from the API | |
returns: a list of dictionaries with time processed to datetime objects | |
""" | |
mapper = {"IT": "%B %d, %Y %H:%M", "TH": "%B %d, %Y %H:%M", "TOI": "%b %d, %Y, %H:%M", | |
"NDTV": "%B %d, %Y %H:%M", "TIE": "%B %d, %Y %H:%M:%S"} | |
for i, article in enumerate(articles): | |
time_strformat = mapper[article["source"]] | |
time = article["time"] | |
time_conv = datetime.strptime(time, time_strformat) | |
articles[i]["time"] = time_conv | |
return articles |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment