1. <legend id='AWWEE'><style id='AWWEE'><dir id='AWWEE'><q id='AWWEE'></q></dir></style></legend>
      <bdo id='AWWEE'></bdo><ul id='AWWEE'></ul>
  2. <i id='AWWEE'><tr id='AWWEE'><dt id='AWWEE'><q id='AWWEE'><span id='AWWEE'><b id='AWWEE'><form id='AWWEE'><ins id='AWWEE'></ins><ul id='AWWEE'></ul><sub id='AWWEE'></sub></form><legend id='AWWEE'></legend><bdo id='AWWEE'><pre id='AWWEE'><center id='AWWEE'></center></pre></bdo></b><th id='AWWEE'></th></span></q></dt></tr></i><div id='AWWEE'><tfoot id='AWWEE'></tfoot><dl id='AWWEE'><fieldset id='AWWEE'></fieldset></dl></div>

      <small id='AWWEE'></small><noframes id='AWWEE'>

    1. <tfoot id='AWWEE'></tfoot>

      使用 strptime 将带偏移量的时间戳转换为 datetime

      时间:2023-09-14

        <i id='y2uA8'><tr id='y2uA8'><dt id='y2uA8'><q id='y2uA8'><span id='y2uA8'><b id='y2uA8'><form id='y2uA8'><ins id='y2uA8'></ins><ul id='y2uA8'></ul><sub id='y2uA8'></sub></form><legend id='y2uA8'></legend><bdo id='y2uA8'><pre id='y2uA8'><center id='y2uA8'></center></pre></bdo></b><th id='y2uA8'></th></span></q></dt></tr></i><div id='y2uA8'><tfoot id='y2uA8'></tfoot><dl id='y2uA8'><fieldset id='y2uA8'></fieldset></dl></div>

          <legend id='y2uA8'><style id='y2uA8'><dir id='y2uA8'><q id='y2uA8'></q></dir></style></legend>
              <tbody id='y2uA8'></tbody>

              <tfoot id='y2uA8'></tfoot>

              <small id='y2uA8'></small><noframes id='y2uA8'>

                <bdo id='y2uA8'></bdo><ul id='y2uA8'></ul>
              • 本文介绍了使用 strptime 将带偏移量的时间戳转换为 datetime obj的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我正在尝试转换格式为2012-07-24T23:14:29-07:00"的时间戳使用 strptime 方法到 python 中的日期时间对象.问题在于最后的时间偏移(-07:00).没有偏移我可以成功

                I am trying to convert time-stamps of the format "2012-07-24T23:14:29-07:00" to datetime objects in python using strptime method. The problem is with the time offset at the end(-07:00). Without the offset i can successfully do

                time_str = "2012-07-24T23:14:29"
                
                time_obj=datetime.datetime.strptime(time_str,'%Y-%m-%dT%H:%M:%S')
                

                但是我尝试了偏移量

                time_str = "2012-07-24T23:14:29-07:00"
                
                time_obj=datetime.datetime.strptime(time_str,'%Y-%m-%dT%H:%M:%S-%z').
                

                但它给出了一个值错误,说z"是一个错误的指令.

                But it gives a Value error saying "z" is a bad directive.

                有什么解决办法吗?

                推荐答案

                Python 2 strptime() 函数确实不支持 %z 格式的时区(因为底层 time.strptime() 函数不支持).你有两个选择:

                The Python 2 strptime() function indeed does not support the %z format for timezones (because the underlying time.strptime() function doesn't support it). You have two options:

                • 使用strptime解析时忽略时区:

                time_obj = datetime.datetime.strptime(time_str[:19], '%Y-%m-%dT%H:%M:%S')
                

              • 使用 dateutil 模块,它是解析函数确实处理时区:

                from dateutil.parser import parse
                time_obj = parse(time_str)
                

              • 命令提示符下的快速演示:

                Quick demo on the command prompt:

                >>> from dateutil.parser import parse
                >>> parse("2012-07-24T23:14:29-07:00")
                datetime.datetime(2012, 7, 24, 23, 14, 29, tzinfo=tzoffset(None, -25200))
                

                您还可以升级到 Python 3.2 或更高版本,其中的时区支持已经改进到 %z 可以工作,前提是您从输入,以及 %z 之前的 -:

                You could also upgrade to Python 3.2 or newer, where timezone support has been improved to the point that %z would work, provided you remove the last : from the input, and the - from before the %z:

                >>> import datetime
                >>> time_str = "2012-07-24T23:14:29-07:00"
                >>> datetime.datetime.strptime(time_str, '%Y-%m-%dT%H:%M:%S%z')
                Traceback (most recent call last):
                  File "<stdin>", line 1, in <module>
                  File "/Users/mj/Development/Library/buildout.python/parts/opt/lib/python3.4/_strptime.py", line 500, in _strptime_datetime
                    tt, fraction = _strptime(data_string, format)
                  File "/Users/mj/Development/Library/buildout.python/parts/opt/lib/python3.4/_strptime.py", line 337, in _strptime
                    (data_string, format))
                ValueError: time data '2012-07-24T23:14:29-07:00' does not match format '%Y-%m-%dT%H:%M:%S%z'
                >>> ''.join(time_str.rsplit(':', 1))
                '2012-07-24T23:14:29-0700'
                >>> datetime.datetime.strptime(''.join(time_str.rsplit(':', 1)), '%Y-%m-%dT%H:%M:%S%z')
                datetime.datetime(2012, 7, 24, 23, 14, 29, tzinfo=datetime.timezone(datetime.timedelta(-1, 61200)))
                

                这篇关于使用 strptime 将带偏移量的时间戳转换为 datetime obj的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如何将带符号的 32 位 int 转换为无符号的 32 位 下一篇:Pandas Timedelta 以天为单位

                相关文章

                最新文章

                <i id='Pu9zF'><tr id='Pu9zF'><dt id='Pu9zF'><q id='Pu9zF'><span id='Pu9zF'><b id='Pu9zF'><form id='Pu9zF'><ins id='Pu9zF'></ins><ul id='Pu9zF'></ul><sub id='Pu9zF'></sub></form><legend id='Pu9zF'></legend><bdo id='Pu9zF'><pre id='Pu9zF'><center id='Pu9zF'></center></pre></bdo></b><th id='Pu9zF'></th></span></q></dt></tr></i><div id='Pu9zF'><tfoot id='Pu9zF'></tfoot><dl id='Pu9zF'><fieldset id='Pu9zF'></fieldset></dl></div>
                <tfoot id='Pu9zF'></tfoot>

                <legend id='Pu9zF'><style id='Pu9zF'><dir id='Pu9zF'><q id='Pu9zF'></q></dir></style></legend>
                1. <small id='Pu9zF'></small><noframes id='Pu9zF'>

                  • <bdo id='Pu9zF'></bdo><ul id='Pu9zF'></ul>