首先添加代碼:如何在python3中將str對象更改爲datetime.time?
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'的時間的補救措施是什麼?
製作是因爲'.strftime( '%H:%M')''中O_all = datetime.strptime(行[6],'%H:%M ').strftime('%H:%M')' – amarnath
@amarnath當我刪除'.strftime('%H:%M')時,錯誤改變爲TypeError:combine()參數2必須是datetime。時間,而不是datetime.datetime'。 – Sourav