Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
了解iso 8061到datetime Python_Python_Datetime_Iso - Fatal编程技术网

了解iso 8061到datetime Python

了解iso 8061到datetime Python,python,datetime,iso,Python,Datetime,Iso,我有以下字符串作为输入: input = "20170620015620.222+0000" 要将其转换为datetime,我可以使用如下内容: utc_format_regex = re.compile('(?P<year>\d{4})' '(?P<month>\d{2})' '(?P<day>\d{2})

我有以下字符串作为输入:

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()
只需使用strtime:


针对python 2用户的警告:%f可能受支持,也可能不受支持,具体取决于您的操作系统和解释器。%z在“我的版本”或“服务器版本”下不受支持。不能这样做,这是你的2.7蟒蛇,为什么%z不工作。运气不好,你可能需要pytz模块。看起来,你可以做smth,比如:“tz=inttz[1://1000*3600”“datetimeyear=intyear,month=intmonth,day=intday,hour=inthour,minute=intminute,second=intsecond,microsecond=intMilisond*1000,tzinfo=dateutil.tz.tzoffsetNone,tz”我不知道,但是,负值会发生什么呢。。。它应该是负数吗?输入不应该用作变量名它不是。我只是在这里打的。尽管如此,这是一个很好的观点
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)
import datetime
parsed = datetime.datetime.strptime(input, '%Y%m%d%H%M%S.%f%z')