2016-11-11 75 views
0

当第三方API返回我包含日期时间戳这样一个CSV数据忽略来自日期时间戳未转换的数据:使用strptime

dtval = '2016-10-14 05:09:30+00:00' 

我必须将它转换格式:mm/dd/yyyy

在这里,我不知道最后+ XX:

datetime.datetime.strptime(dtval, "%Y-%m-%d %H:%M:%S+XX:XX").strftime('%m/%d/%Y') 

我试着以下但不工作:指令XX

>>>datetime.datetime.strptime('2016-10-14 05:09:30+00:00', '%Y-%m-%d %H:%M:%S') 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "/usr/lib/python3.5/_strptime.py", line 500, in _strptime_datetime 
    tt, fraction = _strptime(data_string, format) 
    File "/usr/lib/python3.5/_strptime.py", line 340, in _strptime 
    data_string[found.end():]) 
ValueError: unconverted data remains: +00:00 


>>>datetime.datetime.strptime('2016-10-14 05:09:30+00:00', "%Y-%m-%d %H:%M:%S%z") 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "/usr/lib/python3.5/_strptime.py", line 500, in _strptime_datetime 
    tt, fraction = _strptime(data_string, format) 
    File "/usr/lib/python3.5/_strptime.py", line 337, in _strptime 
    (data_string, format)) 
ValueError: time data '2016-10-14 05:09:30+00:00' does not match format '%Y-%m-%d %H:%M:%S+%z' 

有没有在Python3.4任何选项+的日期时间模块忽略未转换的未转换数据?

我通过this走了,但没有发现任何这样的选择

回答

0

一点点研究,我发现此修复程序在Django的源代码后:

class FixedOffset(tzinfo): 
    """ 
    Fixed offset in minutes east from UTC. Taken from Python's docs. 
    Kept as close as possible to the reference version. __init__ was changed 
    to make its arguments optional, according to Python's requirement that 
    tzinfo subclasses can be instantiated without arguments. 
    """ 

    def __init__(self, offset=None, name=None): 
     if offset is not None: 
      self.__offset = timedelta(minutes=offset) 
     if name is not None: 
      self.__name = name 

    def utcoffset(self, dt): 
     return self.__offset 

    def tzname(self, dt): 
     return self.__name 

    def dst(self, dt): 
     return timedelta(0) 
    def get_timezone(offset): 
     """ 
     Returns a tzinfo instance with a fixed offset from UTC. 
     """ 
     if isinstance(offset, timedelta): 
      offset = offset.seconds // 60 
     sign = '-' if offset < 0 else '+' 
     hhmm = '%02d%02d' % divmod(abs(offset), 60) 
     name = sign + hhmm 
     return FixedOffset(offset, name) 


def custom_strptime(self, value): 
    """Parses a string and return a datetime.datetime. 
    This function supports time zone offsets. When the input contains one, 
    the output uses a timezone with a fixed offset from UTC. 
    Raises ValueError if the input is well formatted but not a valid datetime. 
    Returns None if the input isn't well formatted. 
    """ 
    datetime_re = re.compile(
     r'(?P<year>\d{4})-(?P<month>\d{1,2})-(?P<day>\d{1,2})' 
     r'[T ](?P<hour>\d{1,2}):(?P<minute>\d{1,2})' 
     r'(?::(?P<second>\d{1,2})(?:\.(?P<microsecond>\d{1,6})\d{0,6})?)?' 
     r'(?P<tzinfo>Z|[+-]\d{2}(?::?\d{2})?)?$' 
    ) 
    match = datetime_re.match(value) 
    if match: 
     kw = match.groupdict() 
     if kw['microsecond']: 
      kw['microsecond'] = kw['microsecond'].ljust(6, '0') 
     tzinfo = kw.pop('tzinfo') 
     if tzinfo == 'Z': 
      tzinfo = utc 
     elif tzinfo is not None: 
      offset_mins = int(tzinfo[-2:]) if len(tzinfo) > 3 else 0 
      offset = 60 * int(tzinfo[1:3]) + offset_mins 
      if tzinfo[0] == '-': 
       offset = -offset 
      tzinfo = get_timezone(offset) 
      kw = {k: int(v) for k, v in kw.items() if v is not None} 

     kw['tzinfo'] = tzinfo 
     return datetime.datetime(**kw)