我相信这个问题有一个非常简单的解决方案,但我对Python还比较陌生。
我正在尝试使用dateutil.parser.parse()函数来解析带有时间戳的字符串:
>>> 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
在我的Linux服务器上,这个功能很好用。但在我的Windows测试机上,它会出错:
>>> 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
如果我尝试给dateutil.parser.parse()一个unicode字符串,在Windows系统上也无法工作:
>>> 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'
这在 Linux 系统上同样有效。