如何在python3中将str对象更改为datetime.time?

时间:2017-04-14 06:22:57

标签: python python-3.x datetime

首先添加代码:

import csv
from datetime import datetime, date, time

with open('T2.csv') as readcsvfile:
    readcsv=csv.reader(readcsvfile)
    header=next(readcsv)

    data=[]
    for row in readcsv:
        # if-else construct to read both empty & time string       
        if row[0]==str():
            date=str()
        else:date=datetime.strptime(row[0],'%y%m%d')
        # stripping the 170101 part with str(ing)p(arse)time and
        # changing the style/format into '%Y/%m/%d' format with strftime.

        ID=str(row[5])

        if row[6]==str():
            O_all=str()
        else:O_all=datetime.strptime(row[6],'%H:%M').strftime('%H:%M')

        Combined_datetime=datetime.combine(date,O_all)
        data.append([Combined_datetime,ID])
  print(data)

产生错误:

Combined_datetime=datetime.combine(date,O_all)
TypeError: combine() argument 2 must be datetime.time, not str

但是,如果我检查类型,“日期”和& “O_all”是“datetime.datetime”对象。我想我错过了什么或者错误地理解了什么。获得一个名为'Combined_datetime'的时​​间元组可能有什么办法?

2 个答案:

答案 0 :(得分:0)

使用此代码进行更新

import csv
from datetime import datetime, date, time

with open('T2.csv') as readcsvfile:
    readcsv=csv.reader(readcsvfile)
    header=next(readcsv)

    data=[]
    Combined_datetime = 
    for row in readcsv:

        if len(row[0])<0:
            date=str()
        else:
            date=datetime.strptime(row[0],'%y%m%d')

        ID=str(row[5])

        if len(row[6])<0:
            O_all=str()
        else:
            O_all=datetime.strptime(row[6],'%H:%M').time()

        if date and O_all:
            Combined_datetime = datetime.combine(date, O_all).strftime('%y%m%d %H:%M')
            data.append([Combined_datetime,ID])
        else:
            data.append(['',ID])
    print(data)

您可以探索更多pythonic编写代码的方式。上面的代码看起来很基本,对不起。

答案 1 :(得分:0)

问题出在其他地方。在阅读我的csv时,我可以使用简单的if-else构造读取所有行(空时被视为空字符串)。但是在结合日期和时间的同时使用datetime.combine(d,t)的时间,它无法处理空字符串。 @amarnath你提出的添加.time()的建议虽然有所帮助。这次我用空的时间删除了行。实际上它以这种方式覆盖了TypeError: combine() argument 2 must be datetime.time, not str错误。 这是完整的工作代码:

#Reading T2 & writing new csv

import csv
from datetime import datetime, date, time

with open('T2.csv') as readcsvfile:
    readcsv=csv.reader(readcsvfile)
    header=next(readcsv)

    data=[]
    for row in readcsv:
    #if-else construct to read both empty & time string       
        if row[6]!=str():
            d=datetime.strptime(row[0],'%y%m%d')
            t=datetime.strptime(row[6],'%H:%M').time()
        print(type(d))
    ##stripping the 170101 part with str(ing)p(arse)time and
    ##changing t from datetime.datetime to datetime.time with `.time()`
        print(type(t))
        ID=str(row[5])

        Combined_datetime = datetime.combine(d, t)
        data.append([Combined_datetime,ID])
        print(data)

with open('T2_w3.csv','w',newline='') as writecsvfile:
    writecsv=csv.writer(writecsvfile)
    writecsv.writerow(['Combined_datetime','ID'])

    for i in range(len(data)):
        ROW=data[i]
        CDT=ROW[0]
        ID=ROW[1]
        Final_list=[CDT,ID]
        writecsv.writerow(Final_list)