gpt4 book ai didi

python - Django dateutil ISO 8601 没有 'read' 属性错误

转载 作者:太空宇宙 更新时间:2023-11-03 14:27:52 25 4
gpt4 key购买 nike

我在 Django View 中将日期时间转换为 ISO 8601 以推送到 Facebook 页面事件。我试过 datetime.strptime 但是 following best advice我选择了:

    eventdate = event.date
print eventdate
date_iso = dateutil.parser.parse(eventdate)

当我打印 2013-06-18 02:50:00 时,eventdate 在控制台中看起来像这样,但在我得到之前它没有进一步:

Django Version: 1.4.1
Exception Type: AttributeError
Exception Value:
'datetime.datetime' object has no attribute 'read'
Exception Location: /Users/mirth/.virtualenvs/ssc/lib/python2.7/site-packages/dateutil/parser.py in get_token, line 72
Python Executable: /Users/mirth/.virtualenvs/ssc/bin/python
Python Version: 2.7.2
Python Path:
['/Users/mirth/code/django/ssc',
'/Users/mirth/.virtualenvs/ssc/lib/python2.7/site-packages/requests/packages',
'/Users/mirth/code/django/ssc/main',
'/Users/mirth/code/django/ssc/main/../',
'/Users/mirth/code/django/ssc',
'/Users/mirth/.virtualenvs/ssc/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg',
'/Users/mirth/.virtualenvs/ssc/lib/python2.7/site-packages/pip-1.0.2-py2.7.egg',
'/Users/mirth/.virtualenvs/ssc/lib/python27.zip',
'/Users/mirth/.virtualenvs/ssc/lib/python2.7',
'/Users/mirth/.virtualenvs/ssc/lib/python2.7/plat-darwin',
'/Users/mirth/.virtualenvs/ssc/lib/python2.7/plat-mac',
'/Users/mirth/.virtualenvs/ssc/lib/python2.7/plat-mac/lib-scriptpackages',
'/Users/mirth/.virtualenvs/ssc/lib/python2.7/lib-tk',
'/Users/mirth/.virtualenvs/ssc/lib/python2.7/lib-old',
'/Users/mirth/.virtualenvs/ssc/lib/python2.7/lib-dynload',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
'/Users/mirth/.virtualenvs/ssc/lib/python2.7/site-packages',
'/Users/mirth/.virtualenvs/ssc/lib/python2.7/site-packages/PIL']

这会是什么?谢谢

最佳答案

我可以像这样重现错误消息:

In [58]: import datetime as DT

In [59]: eventdate = DT.datetime(2013, 6, 18, 2, 50)
In [60]: print(eventdate)
2013-06-18 02:50:00

In [61]: import dateutil.parser as parser
In [62]: parser.parse(eventdate)
AttributeError: 'datetime.datetime' object has no attribute 'read'

因此在此基础上,似乎 eventdate 可能已经是一个 datetime.datetime 对象。如果是,则无需调用

date_iso = dateutil.parser.parse(eventdate)

要将其转换为 ISO8601 格式的 string,请使用

In [66]: eventdate.isoformat()
Out[66]: '2013-06-18T02:50:00'

关于python - Django dateutil ISO 8601 没有 'read' 属性错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17077131/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com