datetime - In Python, how do you read in the date (dd/mm/yyyy) from a list and change it to just the name of the month? -
['time period >>', '2/23/2015', '3/2/2015', '3/9/2015', '3/16/2015', '3/23/2015', '3/30/2015', '4/6/2015', '4/13/2015'] how take list , read in dates , change them month written out, example, 2/23/2015 read february or 3/2/2015 read march?
the dates change thinking reading in first number , making if statement change month corresponding number.
we going use strptime convert strings datetime objects. we'll convert strings showing month using strftime.
this easier building dictionary of month/numeral combinations. require of date strings have same format though. assumption is mm/dd/yyyy
import datetime d in s[1:]: try: print datetime.datetime.strptime(d, "%m/%d/%y").strftime("%b") except valueerror: # not datetime string in expected format...do else, or ignore february march march march march march april april here can see looping through list of strings, ignoring first value (because it's not date in example). each converted datetime month, displayed string.
Comments
Post a Comment