[英]TypeError: strptime() argument 1 must be str, not datetime.date Python
Facing issues while converting date to string, 在将日期转换为字符串时遇到问题,
print(type(from_date))
from_date = datetime.datetime.strptime(from_date, '%Y-%m-%d').date()
My from_date value is in an ini file, 我的from_date值在一个ini文件中,
from_date = 2018-01-01
My TraceBack log is, 我的TraceBack日志是
Traceback (most recent call last):
File "Flexi_DailyToWeekly.py", line 87, in <module>
tuesday = get_data(session,from_keyspace, from_table, to_keyspace, to_table_tuesday, to_table_wednesday, to_table_thursday, to_table_friday, from_date, to_date)
File "Flexi_DailyToWeekly.py", line 11, in get_data
from_date = datetime.datetime.strptime(from_date, '%Y-%m-%d')
TypeError: strptime() argument 1 must be str, not datetime.date'
I've used type(from_date) which returns from_date as a string. 我用过type(from_date),它以字符串形式返回from_date。
Also tried, 也尝试过
from_date = datetime.datetime.strptime(str(from_date), '%Y-%m-%d').date()
still the same error persists. 仍然存在相同的错误。
Changed from_date as suggested in the answers below, 根据以下答案中的建议更改了from_date,
from_date = "2018-01-01"
The error persisits, 错误持续存在,
Traceback (most recent call last):
File "Flexi_DailyToWeekly.py", line 82, in <module>
from_date = datetime.datetime.strptime(from_date, '%Y-%m-%d').date()
File "/usr/lib/python3.5/_strptime.py", line 510, in _strptime_datetime
tt, fraction = _strptime(data_string, format)
File "/usr/lib/python3.5/_strptime.py", line 343, in _strptime
(data_string, format))
ValueError: time data '"2018-01-01"' does not match format '%Y-%m-%d'
Replace 更换
from_date = 2018-01-01
to 至
from_date : 2018-01-01
in ini file 在ini文件中
Tested with code : 经过测试的代码:
import configparser
import datetime
config = configparser.ConfigParser()
config.read('Test.ini')
print (config['DEFAULT']['date'])
from_date = config['DEFAULT']['date']
print(type(from_date))
from_date = datetime.datetime.strptime(from_date, '%Y-%m-%d').date()
print (from_date)
and Test.ini is 而Test.ini是
[DEFAULT]
date : 2018-1-1
output is : 输出是:
2018-1-1
<class 'str'>
2018-01-01
As per docs , The datetime.strptime()
method accepts a date_string
as the first argument. 根据docs ,
datetime.strptime()
方法接受date_string
作为第一个参数。 So, I tried this: 所以,我尝试了这个:
import datetime
test = datetime.datetime.strptime('2018-01-01', '%Y-%m-%d').date()
print(test)
#datetime.date(2018, 1, 1)
If I do from_date = 2018-01-01
, I get an invalid token
error for the type
of from_date
: 如果我执行
from_date = 2018-01-01
,则会收到来自from_date
type
的invalid token
错误:
from_date = 2018-01-01
print(type(from_date))
File "<ipython-input-7-9e5449277912>", line 2
from_date = 2018-01-01
^
SyntaxError: invalid token
This means that from_date
must be a string. 这意味着
from_date
必须为字符串。 You could use from_date = '2018-01-01'
and that would work. 您可以使用
from_date = '2018-01-01'
并且可以正常工作。 If I check the type of from_date
now, I will get a type of class string
: 如果现在检查
from_date
的类型,我将得到类string
的类型:
from_date = '2018-01-01'
print(type(from_date))
#<class 'str'>
But when I look at the first Traceback log, it raises a TypeError
. 但是,当我查看第一个Traceback日志时,它将引发
TypeError
。 This means that the value stored in from_date
is of type datetime.date
. 这意味着
from_date
存储的值的类型为datetime.date
。 Consider the following: 考虑以下:
import datetime
from_date = datetime.date(2018, 1, 1)
test = datetime.datetime.strptime(str(from_date), '%Y-%m-%d').date()
print(test)
#2018-01-01
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.