Value error: time data '8' does not match format '%m/%d/%Y %H:%M

Screen Link:

My Code:

result_list = []
for row in ask_posts:
    created_at = row[6]
    comments_at = int(row[4])

counts_by_hour = {}
comments_by_hour = {}
date_format = '%m/%d/%Y %H:%M'
for row in result_list:
    date = row[0]
    comment = row[1]
    time = dt.datetime.strptime(date, date_format).strftime('%H')
    if time in counts_by_hour:
        comments_by_hour[time] += comment
        counts_by_hour[time] += 1
        comments_by_hour[time] = comment
        counts_by_hour[time] = 1

What I expected to happen:
'Generate the frequency table for number of ask_post at any given hour and number of comments on that hour

What actually happened:
Value error: time data ‘8’ does not match format ‘%m/%d/%Y %H:%M’

Replace this line with the output/error
```ValueErrorTraceback (most recent call last)
<ipython-input-18-db80c00fe949> in <module>()
     14     date = row[0]
     15     comment = row[1]
---> 16     time = dt.datetime.strptime(date, date_format).strftime('%H')
     17     row[0]=date
     18     if time in counts_by_hour:

/usr/lib/python3.4/ in _strptime_datetime(cls, data_string, format)
    498     """Return a class cls instance based on the input string and the
    499     format string."""
--> 500     tt, fraction = _strptime(data_string, format)
    501     tzname, gmtoff = tt[-2:]
    502     args = tt[:6] + (fraction,)

/usr/lib/python3.4/ in _strptime(data_string, format)
    335     if not found:
    336         raise ValueError("time data %r does not match format %r" %
--> 337                          (data_string, format))
    338     if len(data_string) != found.end():
    339         raise ValueError("unconverted data remains: %s" %

ValueError: time data '8' does not match format '%m/%d/%Y %H:%M'

<!--Enter other details below: -->

Hi @gsamahita: Please provide a question link as per these guidelines.

Basics (1).ipynb (16.2 KB)

Well, I finally end up getting the right solution from Doyinsolami Olaoye (ValueError and datetime not making sense). Next time, I would follow the steps carefully.
Highly appreciate for your soonest response.
Thank you so much.

Click here to view the jupyter notebook file in a new tab

1 Like

No worries @gsamahita! It is always good to search for similar articles/posts made by others with similar issues/errors. It also saves time on your side instead of awaiting a reply!

Happy Pythoning! Cheers!