praneethng praneethng - 7 days ago 6
Python Question

python script to key in the date range from a file and redirect the output

content was not related to the words keyed in

Answer

Indentation is wrong so your loop does nothing useful. Only last line is processed in the end (s is the match for the last line):

fix: indent properly from 2nd line:

for line in t:
    s = p.match(line)
    if s:
        sdate_str = s.group("date")
        # sort the dates to get only ones between range
        sdate = datetime.datetime.strptime(sdate_str, "%Y%m%d")
        if sdate >= lbound_date and sdate <= rbound_date:
            out.append(line)

(sorry for such a trivial answer)

Comments