Use parameter extension:
newdate = datetime.datetime(*values)
The syntax *expression in the call displays the result of expression as a sequence and applies each element as a separate argument.
This will work for any sequence with a length of 3 to 8 elements, provided that the first 7 are integers and the eighth, if present, is a subclass of datetime.tzinfo .
Combined with mapping to int :
values = ['2014', '08', '17', '18', '01', '05'] newdate = datetime.datetime(*map(int, values))
Demo:
>>> import datetime >>> values = ['2014', '08', '17', '18', '01', '05'] >>> datetime.datetime(*map(int, values)) datetime.datetime(2014, 8, 17, 18, 1, 5)
source share