【发布时间】:2017-11-25 12:05:50
【问题描述】:
我输入以下字符串:
input = "20170620015620.222+0000"
要将其转换为datetime,我可以使用如下内容:
utc_format_regex = re.compile('(?P<year>\d{4})'
'(?P<month>\d{2})'
'(?P<day>\d{2})'
'(?P<hour>\d{2})'
'(?P<minute>\d{2})'
'(?P<second>\d{2})'
'\.'
'(?P<milisecond>\d{3})'
'(?P<tz>\+\d{4})')
year, month, day, hour, minute, second, milisecond, tz = utc_format_regex.match(value).groups()
将其解析为 datetime 似乎很容易,除了 param tzinfo (我不知道如何正确使用它)。该代码不会因为这样的错误而工作:
TypeError: tzinfo argument must be None or of a tzinfo subclass, not type 'unicode'
datetime(year=int(year), month=int(month), day=int(day), hour=int(hour), minute=int(minute), second=int(second),
microsecond=int(milisecond) * 1000, tzinfo=tz)
【问题讨论】:
-
input不应用作变量名 -
不是。我只是在这里输入的。不过好点