【问题标题】:TypeError: strptime() argument 1 must be str, not datetime.date PythonTypeError: strptime() 参数 1 必须是 str,而不是 datetime.date Python
【发布时间】:2018-09-04 05:28:38
【问题描述】:

在将日期转换为字符串时遇到问题,

print(type(from_date))
from_date = datetime.datetime.strptime(from_date, '%Y-%m-%d').date()

我的 from_date 值在一个 ini 文件中,

from_date = 2018-01-01

我的 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'

我使用了 type(from_date),它以字符串形式返回 from_date。

也试过了,

from_date = datetime.datetime.strptime(str(from_date), '%Y-%m-%d').date()

同样的错误仍然存​​在。

按照以下答案中的建议更改了 from_date,

from_date = "2018-01-01"

错误仍然存​​在,

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'

【问题讨论】:

  • 使用 str(from_date)
  • 也试过了。没用
  • 没有。您的错误不会持续存在,它已更改。现在它表明您已设法在字符串 ('"2018-01-01"') 中包含双引号,这是您最新错误的原因。为了让人们提供帮助,您将不得不展示更多的代码,而不仅仅是回溯和它出现的行 - 看看如何编写 minimal reproducible example
  • 尝试使用单引号而不是双引号,例如 from_date = '2018-01-01'

标签: python datetime


【解决方案1】:

替换

from_date = 2018-01-01

from_date : 2018-01-01

在ini文件中

用代码测试:

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)

Test.ini 是

[DEFAULT]
date : 2018-1-1

输出是:

2018-1-1
<class 'str'>
2018-01-01

【讨论】:

【解决方案2】:

根据docsdatetime.strptime() 方法接受date_string 作为第一个参数。所以,我尝试了这个:

import datetime
test = datetime.datetime.strptime('2018-01-01', '%Y-%m-%d').date()
print(test)
#datetime.date(2018, 1, 1)

如果我执行from_date = 2018-01-01,我会收到typetypeinvalid 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

这意味着from_date 必须是一个字符串。你可以使用from_date = '2018-01-01',这样就可以了。如果我现在检查from_date的类型,我会得到一个类string的类型:

from_date = '2018-01-01'
print(type(from_date))
#<class 'str'>

但是当我查看第一个 Traceback 日志时,它会引发 TypeError。这意味着存储在from_date 中的值是datetime.date 类型。考虑以下几点:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-02
    • 2020-07-11
    • 2022-11-21
    • 1970-01-01
    • 1970-01-01
    • 2019-03-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多