python datetime, convert weekday's abbrv to number?
How would i go about converting a weekday abbreviation (%a) into the weekday number (%u) using python d开发者_如何学JAVAatetime module?
>>> import calendar
>>> zip(list(calendar.day_abbr), range(7))
[('Mon', 0), ('Tue', 1), ('Wed', 2), ('Thu', 3), ('Fri', 4), ('Sat', 5), ('Sun', 6)]
Better use the time.strptime function, which depends on the locale. If it is set to English (usually by default), you should get
>>> import time
>>> time.strptime("Tue", '%a').tm_wday
>>> 1
Call the datetime
module with an arbitrary date and save the results for the conversions for %a
and %u
in a map. %a
is the key for this map, %u
is the value.
Increment the date by one day.
Repeat 7 times.
Now you have a map which returns the value you seek for any possible value of %a
.
精彩评论