我试图将我的工作日作为字符串转换为python中的int,这就是我所拥有的
$(".acol").each(function(i) {
console.log($(this).html());
});
但是我得到了这个错误
from datetime import date, datetime, time, time delta
from pytz import timezone
now = datetime.now(timezone('UTC'))
dt_tz = now.astimezone(timezone('US/Eastern'))
dt = dt_tz.replace(tzinfo=None)
weekday = 'Friday'
weekday_as_int = dt.strptime(weekday, "%A")
为什么不将ValueError: time data 'Friday' does not match format '%A'
更改为int?
答案 0 :(得分:4)
整个工作日的正确格式为%A
:
import time
weekday_as_int = time.strptime('friday', "%A").tm_wday
>>> weekday_as_int.tm_wday
4
星期一的计数从零开始:
>>> time.strptime('Monday', "%A").tm_wday
0
一些时间:
@Rustem的版本:
%%timeit
days = dict(zip(calendar.day_name, range(7)));
days['Friday']
10000 loops, best of 3: 104 µs per loop
此版本:
%timeit time.strptime('Friday', "%A").tm_wday
10000 loops, best of 3: 19.7 µs per loop
看起来strptime
的速度提高了五倍。
答案 1 :(得分:2)
最快的方式是:
import calendar
days = dict(zip(calendar.day_name, range(7)));
days['Friday']
是的,strptime很慢,对于这种微不足道的操作来说太过分了。
与@Mike版本相比
In [7]: timeit time.strptime('Friday', "%A").tm_wday
The slowest run took 337.46 times longer than the fastest. This could mean that an intermediate result is being cached
100000 loops, best of 3: 7.14 µs per loop
In [8]: timeit days['Friday']
The slowest run took 22.05 times longer than the fastest. This could mean that an intermediate result is being cached
10000000 loops, best of 3: 54.1 ns per loop