简体   繁体   English

TypeError:strptime()参数1必须是str,而不是datetime.date Python

[英]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. 根据docsdatetime.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 typeinvalid 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.

相关问题 这个错误的解决方法是什么? TypeError: strptime() argument 1 must be str, not datetime.date - What is a solution to this error TypeError: strptime() argument 1 must be str, not datetime.date combine() 参数 1 必须是 datetime.date,而不是 str - combine() argument 1 must be datetime.date, not str Datetime(TypeError:strptime() 参数 1 必须是 str,而不是 Timestamp) - Datetime (TypeError: strptime() argument 1 must be str, not Timestamp) datetime.strptime:TypeError:strptime()参数1必须是str,而不是Series - datetime.strptime: TypeError: strptime() argument 1 must be str, not Series TypeError:尝试在Python中创建文件时,必须为str,而不是datetime.date - TypeError: must be str, not datetime.date when trying to create a file in Python `TypeError:strptime()参数0必须是str,而不是 <class 'bytes'> ` - `TypeError: strptime() argument 0 must be str, not <class 'bytes'>` 类型错误:strptime() 参数 1 必须是 str,而不是句点 - TypeError: strptime() argument 1 must be str, not Period 类型错误:strptime() 参数 1 必须是 str,而不是 None - TypeError : strptime() argument 1 must be str, not None TypeError: strptime() 参数 1 必须是 str,而不是 Series - TypeError: strptime() argument 1 must be str, not Series TypeError:strptime()参数1必须是str,而不是list - TypeError: strptime() argument 1 must be str, not list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM