TAGS :Viewed: 22 - Published at: a few seconds ago
[ Django dateutil ISO 8601 no 'read' attribute error ]
I am converting a datetime to ISO 8601 in a Django view to push to a Facebook page event. I have tried datetime.strptime
but following best advice I've opted to:
eventdate = event.date
print eventdate
date_iso = dateutil.parser.parse(eventdate)
eventdate looks like this in the console when I print 2013-06-18 02:50:00
but it doesn't get any further before I get:
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']
What could this be? Thanks
Answer 1
I can reproduce the error message like this:
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'
So on this basis it seems as though eventdate
is probably already a datetime.datetime
object. If so, there is no need to call
date_iso = dateutil.parser.parse(eventdate)
To convert it to a string
in ISO8601 format, use
In [66]: eventdate.isoformat()
Out[66]: '2013-06-18T02:50:00'
Answer 2
The error is quite clear: you're passing in a datetime
object when in fact parse
expects a string.