Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dateutil.parser.parse() gives error "initial_value must be unicode or None, not str" on Windows platform

I'm sure there's a really simple solution to this, but I'm still fairly new to Python.

I'm trying to use dateutil.parser.parse() to parse a string with a timestamp in it:

>>> import dateutil.parser
>>> a = dateutil.parser.parse("2011-10-01 12:00:00+01:00")
>>> print a
2011-10-01 12:00:00+01:00

This works fine on my Linux server, but on my Windows test box it gives an error:

>>> import dateutil.parser
>>> a = dateutil.parser.parse("2011-10-01 12:00:00+01:00")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\site-packages\python_dateutil-2.0-py2.7.egg\dateutil\parser.py", line 698, in parse
    return DEFAULTPARSER.parse(timestr, **kwargs)
  File "C:\Python27\lib\site-packages\python_dateutil-2.0-py2.7.egg\dateutil\parser.py", line 302, in parse
    res = self._parse(timestr, **kwargs)
  File "C:\Python27\lib\site-packages\python_dateutil-2.0-py2.7.egg\dateutil\parser.py", line 350, in _parse
    l = _timelex.split(timestr)
  File "C:\Python27\lib\site-packages\python_dateutil-2.0-py2.7.egg\dateutil\parser.py", line 144, in split
    return list(cls(s))
  File "C:\Python27\lib\site-packages\python_dateutil-2.0-py2.7.egg\dateutil\parser.py", line 44, in __init__
    instream = StringIO(instream)
TypeError: initial_value must be unicode or None, not str

If I try giving dateutil.parser.parse() a unicode string, that doesn't work on the Windows box either:

>>> a = dateutil.parser.parse(unicode("2011-10-01 12:00:00+01:00"))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\site-packages\python_dateutil-2.0-py2.7.egg\dateutil\parser.py", line 698, in parse
    return DEFAULTPARSER.parse(timestr, **kwargs)
  File "C:\Python27\lib\site-packages\python_dateutil-2.0-py2.7.egg\dateutil\parser.py", line 302, in parse
    res = self._parse(timestr, **kwargs)
  File "C:\Python27\lib\site-packages\python_dateutil-2.0-py2.7.egg\dateutil\parser.py", line 350, in _parse
    l = _timelex.split(timestr)
  File "C:\Python27\lib\site-packages\python_dateutil-2.0-py2.7.egg\dateutil\parser.py", line 144, in split
    return list(cls(s))
TypeError: iter() returned non-iterator of type '_timelex'

Yet this also works on the Linux box.

like image 590
Steve Criddle Avatar asked Oct 18 '11 08:10

Steve Criddle


1 Answers

It's not a Windows issue, it's Python version / library version issue.

dateutil 2.0 is written to support only Python 3, not Python 2.X. Both cases here contain bugs when used with Python 2.X.

In the first case:

dateutil.parser.parse("2011-10-01 12:00:00+01:00")

the io.StringIO class allows only unicode arguments, but the code reads:

    if isinstance(instream, str):
        instream = StringIO(instream)

In the second case:

dateutil.parser.parse(unicode("2011-10-01 12:00:00+01:00"))

if you look at _timelex class, it contains the __next__ method, which is Python3's way of indicating that an object supports iteration protocol. In Python 2.X, the name of the method should be next.

Check if you have the same versions of both Python and the library on Linux and Windows. From project website:

python-dateutil-2.0.tar.gz (Python >= 3.0)

python-dateutil-1.5.tar.gz (Python < 3.0)

like image 124
DzinX Avatar answered Oct 17 '22 11:10

DzinX