ValueError: format does not match

Screen Link:
Hello, can anyone help me out on this error, i tried many slutions has been suggested by Dataquest community but no thing works :frowning:

My Code:

counts_by_hour={}
comments_by_hour={}

for row in result_list:
    date=row[0]
    hour=dt.datetime.strptime(date, "%m/%d/%Y %H:%M")
    comm_h=hour.strftime("%H")
    if hour not in counts_by_hour:
        counts_by_hour[hour]=1
        comments_by_hour[hour]=row[1]
    else:
        counts_by_hour[hour]+=1
        comments_by_hour[hour]=row[1]

What I expected to happen:

What actually happened:

  4 for row in result_list:
      5     date=row[0]
----> 6     hour=dt.datetime.strptime(date, "%m/%d/%Y %H:%M")
      7     comm_h=hour.strftime("%H")
      8     if hour not in counts_by_hour:

/usr/local/lib/python3.8/_strptime.py in _strptime_datetime(cls, data_string, format)
    566     """Return a class cls instance based on the input string and the
    567     format string."""
--> 568     tt, fraction, gmtoff_fraction = _strptime(data_string, format)
    569     tzname, gmtoff = tt[-2:]
    570     args = tt[:6] + (fraction,)

/usr/local/lib/python3.8/_strptime.py in _strptime(data_string, format)
    347     found = format_regex.match(data_string)
    348     if not found:
--> 349         raise ValueError("time data %r does not match format %r" %
    350                          (data_string, format))
    351     if len(data_string) != found.end():

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

Hi @feda.twj1,

For now I see the only issue. This line

comm_h=hour.strftime("%H")

has to be actually

hour=hour.strftime("%H")

and then we use this hour in the for-loop.

Can you please also share your code for calculating result_list? It seems that there can be some other issues.

Thank you for your correction, but still the error appear.

result_list=
for row in ask_posts:
time=row[6]
com_n=row[4]
com_n=int(com_n)
result_list.append(time)
result_list.append(com_n)

counts_by_hour={}
comments_by_hour={}

for row in result_list:
date=row[0]
hour=dt.datetime.strptime(date, “%m/%d/%Y %H:%M”)
hour=hour.strftime("%H")
if hour in counts_by_hour:
counts_by_hour[hour]=1
comments_by_hour[hour]=row[1]
else:
counts_by_hour[hour]+=1
comments_by_hour[hour]=row[1]

Hello @feda.twj1, kindly give us the screen link for assistance.

Ah, then the issue is exactly with your result_list. Try this:

result_list = []

for row in ask_posts:
    time = row[6]
    com_n = int(row[4])
    result_list.append([time, com_n])

Because you have to create a list of lists, where each “sub-list” contains the time when a post was created and the number of comments it received.

1 Like

Thank you!
I did not realize that…

1 Like