我正在编写一个python代码,用于接受日期的用户输入:
from datetime import datetime
import time
time_input = str(raw_input("Please enter the time in HH:MM:SS format: "))
current_date = str(raw_input("Please enter the date in YYYY/MM/DD format:"))
selected_time = datetime.strptime((current_date, time_input),"%Y/%m/%d %H:%M:%S.%f")
print "Time selected: ",selected_time
我在这一行收到错误:
selected_time = datetime.strptime((current_date, time_input),"%Y/%m/%d %H:%
M:%S.%f")
TypeError:strptime()参数1必须是字符串,而不是元组
答案 0 :(得分:0)
错误表明
TypeError:strptime()参数1必须是字符串,而不是元组
selected_time = datetime.strptime((current_date, time_input),"%Y/%m/%d %H:%M:%S.%f")
第一个参数应该是一个字符串,但是你传递的是(current_date, time_input)
元组。
由于您分别接受时间和日期的输入,因此您可以使用'%s %s' % (current_date, time_input)
加入它们,然后将其传递给datetime.strptime
,如下所示
selected_time = datetime.strptime(
'%s %s' % (current_date, time_input), # first argument is now a string
"%Y/%m/%d %H:%M:%S.%f",
)