如何从CSV解析日期时间?

时间:2019-11-12 11:53:50

标签: python datetime

这是我正在使用line.find的特定行:

Alta,"08-10-2019 16:34:42","08-10-2019 18:03:42","RESOLVIDO","roteador-assu-tely","Host indisponível","1h 29m","Não","",""

很显然,自从完整的CSV文件以来,还有很多其他的行,但是现在让我们关注这一行。 我正在使用split(',')分隔行并尝试使用它生成一个datetime对象,我在做什么错了?

with open(path + "zbx_problems_export.csv", "r") as f:
    lines = f.readlines()
inicio_expediente = datetime.time(6, 40, 00)
fim_expediente = datetime.time(16, 30, 00)

for line in lines:
    if line.find('roteador-assu-tely') >= 0:
        a = line.split(',')
        data_inicio = datetime.datetime.strptime(a[1], '"%d-%m-%Y %H:%M:%S"')
        data_fim = datetime.datetime.strptime(a[2], '"%d-%m-%Y %H:%M:%S"')
        if data_inicio.time > inicio_expediente.time and data_inicio.time < fim_expediente.time and data_inicio.weekday() != 5 and data_inicio.weekday() != 6: 

完整的错误消息是:

Traceback (most recent call last):
  File "ScriptPythonRelatorioProto.py", line 31, in <module>
     if data_inicio.time > inicio_expediente.time and data_inicio.time < fim_expediente.time and data_inicio.weekday() != 5 and data_inicio.weekday() != 6:
AttributeError: 'datetime.time' object has no attribute 'time'

第31行是示例代码中的最后一次发布

1 个答案:

答案 0 :(得分:1)

inicio_expediente = datetime.time(6, 40, 00)
     

[..]

inicio_expediente.time

inicio_expediente 是一个time对象,它没有.time属性。只需在此处删除.time

然后您在这里还有另一个问题:data_inicio.time
data_inicio是一个datetime对象,它一个.time,但这是方法,不是简单的属性,因此必须是data_inicio.time()