简体   繁体   English

ValueError: 时间数据 '' 与格式 '%Y-%m-%dT%H:%M:%S' 不匹配

[英]ValueError: time data '' does not match format '%Y-%m-%dT%H:%M:%S'

I'm getting the preceding traceback error when I run the code - despite having thought I formatted everything correctly.当我运行代码时,我收到了前面的回溯错误 - 尽管我认为我正确地格式化了所有内容。

Here is the traceback这是回溯

File "/alien.py", line 267, in filter_ufo_by_year
return ufo_by_year(filter_sightings(filter_text))
File "/alien.py", line 96, in ufo_by_year
in groupby('shape', sightings).items()
File "/alien.py", line 95, in <dictcomp>
for shape, class_sightings
File "lib/python3.6/site-packages/toolz/recipes.py", line 24, in 
countby
return frequencies(map(key, seq))
File "lib/python3.6/site-packages/toolz/itertoolz.py", line 539, in 
frequencies
for item in seq:
File "/alien.py", line 32, in sighting_year
return dt.datetime.strptime(sighting['date_time'], 
TIMESTAMP_FORMAT).year
File 
"/python/3.6.5/_strptime.py", line 565, in _strptime_datetime
tt, fraction = _strptime(data_string, format)
File 
"/python3.6/_strptime.py", line 362, in _strptime
(data_string, format))
ValueError: time data '' does not match format '%Y-%m-%dT%H:%M:%S'

Here is the relevant code:这是相关的代码:

TIMESTAMP_FORMAT = "%Y-%m-%dT%H:%M:%SZ"

def sighting_year(sighting):
    return dt.datetime.strptime(sighting['date_time'], 
    TIMESTAMP_FORMAT).year

def ufo_by_year(sightings):

    sightings_by_year = {
    shape: 
        sorted(
            list(
                # Group by year -> count.
                countby(sighting_year, class_sightings).items()
            ),
            # Sort by year.
            key=first
        )
    for shape, class_sightings 
    in groupby('shape', sightings).items()
}

# Build the plot with a dictionary.
return {
    "data": [
        {
            "type": "scatter",
            "mode": "lines+markers",
            "name": shape,
            "x": listpluck(0, class_sightings_by_year),
            "y": listpluck(1, class_sightings_by_year)
        }
        for shape, class_sightings_by_year 
        in sightings_by_year.items()
    ],
    "layout": {
        "title": "Sightings by Year",
        "showlegend": False
    }
}

Here is what a sample cell looks like for 'date_time': 2015-07-25T21:45:00以下是“date_time”的示例单元格的样子:2015-07-25T21:45:00

Any help here would be greatly appreciated任何帮助在这里将不胜感激

There's no timezone in the string you've provided:您提供的字符串中没有时区:

>>> sample_datetime = "2015-07-25T21:45:00"
>>> obj = dt.datetime.strptime(sample_datetime, TIMESTAMP_FORMAT)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/mm92400/anaconda3/envs/py36/lib/python3.6/_strptime.py", line 565, in _strptime_datetime
    tt, fraction = _strptime(data_string, format)
  File "/Users/mm92400/anaconda3/envs/py36/lib/python3.6/_strptime.py", line 362, in _strptime
    (data_string, format))
ValueError: time data '2015-07-25T21:45:00' does not match format '%Y-%m-%dT%H:%M:%SZ'

Removing Z from your format string从格式字符串中删除Z

obj = dt.datetime.strptime(sample_datetime, '%Y-%m-%dT%H:%M:%S')
>>> obj 
datetime.datetime(2015, 7, 25, 21, 45)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError:时间数据“”与格式“%Y-%m-%dT%H:%M:%SZ”不匹配 - ValueError: time data '' does not match format '%Y-%m-%dT%H:%M:%SZ' ValueError: 时间数据“无”与格式“%Y-%m-%dT%H:%M:%S.%f”不匹配 - ValueError: time data 'None' does not match format '%Y-%m-%dT%H:%M:%S.%f' ValueError: 时间数据“无”与格式“%Y-%m-%d %H:%M:%S”不匹配 - ValueError: time data 'None' does not match format '%Y-%m-%d %H:%M:%S' ValueError:时间数据“ 140120 1520”与格式“%Y-%m-%d%H:%M:%S”不匹配 - ValueError: time data '140120 1520' does not match format '%Y-%m-%d %H:%M:%S' ValueError: 时间数据与格式 &#39;%Y-%m-%d %H:%M:%S.%f&#39; 不匹配 - ValueError: time data does not match format '%Y-%m-%d %H:%M:%S.%f' 出现错误“ValueError:时间数据&#39;&#39;与格式&#39;%Y-%m-%d %H:%M:%S&#39;不匹配” - Getting error "ValueError: time data '' does not match format '%Y-%m-%d %H:%M:%S'" ValueError: 时间数据 &#39;2020-01-31T15:16:21+00:00&#39; 与格式 &#39;%Y-%m-%dT%H:%M:%S%z&#39; 不匹配 - ValueError: time data '2020-01-31T15:16:21+00:00' does not match format '%Y-%m-%dT%H:%M:%S%z' ValueError: 时间数据 '' 与格式 '%Y-%m-%d %H:%M' 不匹配 - ValueError: time data '' does not match format '%Y-%m-%d %H:%M' backtrader 时间列:ValueError:时间数据“0”与格式“%Y-%m-%d %H:%M:%S”不匹配 - backtrader time column : ValueError: time data '0' does not match format '%Y-%m-%d %H:%M:%S' ValueError:时间数据与远程计算机文件上的格式&#39;%Y-%m-%d%H:%M:%S&#39;不匹配 - ValueError: time data does not match format '%Y-%m-%d %H:%M:%S' on remote machine file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM