使用Python中的datetime.strptime()返回的数据初始化列表

时间:2014-04-22 11:37:55

标签: python datetime

我有python模块:

import datetime
timeList = [
            [".US",datetime.strptime('%H:%M:%S','15:30'), datetime.strptime('%H:%M:%S','22:00')],
            [".PL",datetime.strptime('%H:%M:%S','09:00'), datetime.strptime('%H:%M:%S','16:50')],
            [".CZ",datetime.strptime('%H:%M:%S','09:00'), datetime.strptime('%H:%M:%S','16:20')],
            [".DE",datetime.strptime('%H:%M:%S','09:00'), datetime.strptime('%H:%M:%S','17:30')],
            [".ES",datetime.strptime('%H:%M:%S','09:00'), datetime.strptime('%H:%M:%S','17:30')],
            [".IT",datetime.strptime('%H:%M:%S','09:00'), datetime.strptime('%H:%M:%S','17:30')],
            [".PT",datetime.strptime('%H:%M:%S','09:00'), datetime.strptime('%H:%M:%S','17:30')],
            [".UK",datetime.strptime('%H:%M:%S','09:00'), datetime.strptime('%H:%M:%S','17:30')],
            [".FR",datetime.strptime('%H:%M:%S','09:00'), datetime.strptime('%H:%M:%S','17:30')]]

使用上面提供的数据在另一个模块中编码:

for timeData in tradeHours.timeList:
     ...

我收到了错误:

AttributeError:' module' object没有属性strptime()。

如何解决?

[编辑]: 修复后我的代码如下:

import datetime

INSTRUMENT_TYPE = 0
TIME_BEGIN = 1
TIME_END = 2
timeList = [
            [".US",datetime.datetime.strptime('15:30:00','%H:%M:%S'), datetime.datetime.strptime('22:00:00','%H:%M:%S')],
            [".PL",datetime.datetime.strptime('09:00:00','%H:%M:%S'), datetime.datetime.strptime('16:50:00','%H:%M:%S')],
            [".CZ",datetime.datetime.strptime('09:00:00','%H:%M:%S'), datetime.datetime.strptime('16:20:00','%H:%M:%S')],
            [".DE",datetime.datetime.strptime('09:00:00','%H:%M:%S'), datetime.datetime.strptime('17:30:00','%H:%M:%S')],
            [".ES",datetime.datetime.strptime('09:00:00','%H:%M:%S'), datetime.datetime.strptime('17:30:00','%H:%M:%S')],
            [".IT",datetime.datetime.strptime('09:00:00','%H:%M:%S'), datetime.datetime.strptime('17:30:00','%H:%M:%S')],
            [".PT",datetime.datetime.strptime('09:00:00','%H:%M:%S'), datetime.datetime.strptime('17:30:00','%H:%M:%S')],
            [".UK",datetime.datetime.strptime('09:00:00','%H:%M:%S'), datetime.datetime.strptime('17:30:00','%H:%M:%S')],
            [".FR",datetime.datetime.strptime('09:00:00','%H:%M:%S'), datetime.datetime.strptime('17:30:00','%H:%M:%S')]]

datetime.strptime符合我的要求,并且优于datetime.time,因为我在格式中使用日期'%H:%M:%S'在我的程序中,不需要在很少的地方进行任何转换。

2 个答案:

答案 0 :(得分:3)

你必须这样做:

datetime.datetime.strptime(...)而不是

或者,您可以这样做:

from datetime import datetime

然后你可以这样做:

datetime.strptime(...)

答案 1 :(得分:1)

您导入只是模块datetime但是正在尝试在 datetime.datetime上调用类方法;你要调整你的引用或导入(以及你的解析模式,你的字符串文字中没有秒):

import datetime
datetime.datetime.strptime('%H:%M','15:30')

from datetime import datetime
datetime.strptime('%H:%M','15:30')

但是,由于您只处理小时和分钟,我会使用datetime.time()对象,而只是将其创建为文字:

from datetime import time
timeList = [
    [".US", time(15, 30), time(22)],
    [".PL", time(9),      time(16, 50)],
    [".CZ", time(9),      time(16, 20)],
    [".DE", time(9),      time(17, 30)],
    [".ES", time(9),      time(17, 30)],
    [".IT", time(9),      time(17, 30)],
    [".PT", time(9),      time(17, 30)],
    [".UK", time(9),      time(17, 30)],
    [".FR", time(9),      time(17, 30)]
]