Skip to content

Instantly share code, notes, and snippets.

@rg089
Created July 16, 2021 13:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rg089/022820f3fd1ec82bcab3b749d7195fe3 to your computer and use it in GitHub Desktop.
Save rg089/022820f3fd1ec82bcab3b749d7195fe3 to your computer and use it in GitHub Desktop.
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