Loop Issue on Guided Project: Exploring Hacker News Posts - 5/8

While trying to run a loop through the “result_list” it gives an error if anything else is used other than “row”.
URL: Python Project: Exploring Hacker News Posts | Dataquest

My Code:

for i in result_list:
    date_and_time = row[0]
    date_and_time = dt.datetime.strptime(date_and_time, "%m/%d/%Y %H:%M")
    post_time = date_and_time.strftime("%H")

Expected output:


Actual output:

ValueError                                Traceback (most recent call last)
<ipython-input-16-ff183c171e26> in <module>
     12 for i in result_list:
     13     date_and_time = row[0]
---> 14     date_and_time = dt.datetime.strptime(date_and_time, "%m/%d/%Y %H:%M")
     15     post_time = date_and_time.strftime("%H")

/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 '11743946' does not match format '%m/%d/%Y %H:%M'

The same issue occurs in DQ.io’s notebook virtual environment and in my own separate notebook. This issue is fixed when “i” and switched out for “row” at the beginning of the loop, but my question is why would that be necessary to avoid getting an error?

With given detail can not say specific reason. but by seeing your code i can see potential conflict is you did not change row to i in the loop.

It should be

for i in result_list:
    date_and_time = i[0]

Thank you, that was the issue and changing it solved the problem! Good catch!

1 Like