我寫一個Python代碼,用於接受用戶輸入的日期:類型錯誤:strptime()參數1必須是字符串,而不是元組
我在這條線得到一個錯誤:
selected_time = datetime.strptime((current_date, time_input),"%Y/%m/%d %H:% M:%S.%f")
TypeError: strptime() argument 1 must be string, not tuple
我寫一個Python代碼,用於接受用戶輸入的日期:類型錯誤:strptime()參數1必須是字符串,而不是元組
我在這條線得到一個錯誤:
selected_time = datetime.strptime((current_date, time_input),"%Y/%m/%d %H:% M:%S.%f")
TypeError: strptime() argument 1 must be string, not tuple
作爲錯誤指示
TypeError: strptime() argument 1 must be string, not tuple
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",
)
我已經嘗試過你的解決方案,但它給了我另一個錯誤AttributeError:'method_descriptor'對象沒有屬性'strptime' –
感謝您的幫助,它正在工作nowBelow是更正的代碼selected_time = datetime.strptime('%s %s'%(current_date,time_input),「%Y /%m /%d%H:%M:%S」) –
簡言之:多個參數不會自動級聯。 – TigerhawkT3