w3cdtf_to_datetime is giving 'NoneType' object has no attribute 'group'

177 views
Skip to first unread message

Marcos Vinícius Petri

unread,
Oct 9, 2015, 2:41:23 AM10/9/15
to python-excel
I'm trying to convert a string formated as a w3c date (YYY-MM-DD) to a datetime that excel can use. A simple code would be:

from openpyxl.utils import datetime

print datetime.W3CDTF_to_datetime("1990-04-03")

but when I try to run I receive the following error message:

File "file_path", line 40, in W3CDTF_to_datetime
dt = [int(v) for v in match.groups()[:6]]
AttributeError: 'NoneType' object has no attribute 'groups'

Can someone please tell me what is going on here? 

Charlie Clark

unread,
Oct 9, 2015, 2:46:33 AM10/9/15
to python...@googlegroups.com
Am .10.2015, 02:32 Uhr, schrieb Marcos Vinícius Petri <mvp...@gmail.com>:

> from openpyxl.utils import datetime
> print datetime.W3CDTF_to_datetime("1990-04-03")

That is a date and not a datetime and so cannot be converted to a datetime.

Normally, you will never need to do the conversion yourself. Just create a
Python date, datetime or time object and openpyxl will correctly convert
it into Excel's serial (epoch 1900) format.

Charlie
--
Charlie Clark
Managing Director
Clark Consulting & Research
German Office
Kronenstr. 27a
Düsseldorf
D- 40217
Tel: +49-211-600-3657
Mobile: +49-178-782-6226

Adrian Klaver

unread,
Oct 9, 2015, 8:53:58 AM10/9/15
to python...@googlegroups.com
On 10/08/2015 11:46 PM, Charlie Clark wrote:
> Am .10.2015, 02:32 Uhr, schrieb Marcos Vinícius Petri <mvp...@gmail.com>:
>
>> from openpyxl.utils import datetime
>> print datetime.W3CDTF_to_datetime("1990-04-03")
>
> That is a date and not a datetime and so cannot be converted to a datetime.

Seems to be a bug in W3CDTF_to_datetime, as Python thinks it is a datetime:

In [4]: datetime(1990, 4, 3)
Out[4]: datetime.datetime(1990, 4, 3, 0, 0)

https://docs.python.org/2/library/datetime.html#datetime-objects

class datetime.datetime(year, month, day[, hour[, minute[, second[,
microsecond[, tzinfo]]]]])

The year, month and day arguments are required.

>
> Normally, you will never need to do the conversion yourself. Just create
> a Python date, datetime or time object and openpyxl will correctly
> convert it into Excel's serial (epoch 1900) format.
>
> Charlie


--
Adrian Klaver
adrian...@aklaver.com

Charlie Clark

unread,
Oct 9, 2015, 9:02:42 AM10/9/15
to python...@googlegroups.com
Am .10.2015, 14:53 Uhr, schrieb Adrian Klaver <adrian...@aklaver.com>:

> Seems to be a bug in W3CDTF_to_datetime, as Python thinks it is a
> datetime:

It's not a bug. The function does what's needed to do internally.

Adrian Klaver

unread,
Oct 9, 2015, 9:10:50 AM10/9/15
to python...@googlegroups.com
On 10/09/2015 06:02 AM, Charlie Clark wrote:
> Am .10.2015, 14:53 Uhr, schrieb Adrian Klaver <adrian...@aklaver.com>:
>
>> Seems to be a bug in W3CDTF_to_datetime, as Python thinks it is a
>> datetime:
>
> It's not a bug. The function does what's needed to do internally.

I would disagree and in the theory of least surprise it should follow
the Python and other programs parsing:

In [1]: from dateutil.parser import parse

In [2]: parse("1990-04-03")
Out[2]: datetime.datetime(1990, 4, 3, 0, 0)


Postgres

test=> select '1990-04-03'::timestamp;
timestamp
---------------------
1990-04-03 00:00:00
(1 row)

Charlie Clark

unread,
Oct 9, 2015, 9:21:44 AM10/9/15
to python...@googlegroups.com
Am .10.2015, 15:10 Uhr, schrieb Adrian Klaver <adrian...@aklaver.com>:

> I would disagree

You're welcome to.

For openpyxl it's restricted to a strict conversion of the W3C datetime
format which is always a datetime. Client code doesn't need it: there are
better libraries such as mxDateTime, dateutil or even the stdlib's
strptime() (in Python 3 at least), where general conversion is required.
In openpyxl we're always going between particular formats.
Reply all
Reply to author
Forward
0 new messages