python - How to format the datetime object in list with strftime while leaving following numbers unaffected -
i have typical list
of data, first item datetime object, , following items data corresponding date , time. print , format datetime
object. don't want str
representation of datetime
, rather, choose formatting via strftime
or similar.
i have tried
print('{}'.format(', '.join(map(str,row))))
and works ok, don't want format (from overriding default format when printing list of datetime objects).
i know how use strftime
, don't know how call on data in list
. seems there should easy way this. when searching how perform this, results either strftime
or using str
in list
above, have not been able find trying do.
this leave non-datetime
objects alone (i assume they're strings - if not, use else str(item)
instead of else item
). uses ternary if..else
@ each item in row
.
print(', '.join(datetime.datetime.strftime(item, '%y%m%d') if type(item) == datetime.datetime else item item in row))
the format()
doesn't seem necessary, since you're printing 1 item , not doing formatting.
Comments
Post a Comment