0

I have this code to read a txt file and remove any lines with not starting with "Date".

file1 = open('XXXXXX.txt',
             'r', encoding="ISO-8859-1")

file2 = open('XXXXXX-Update.txt',
             'w', encoding="ISO-8859-1")

for line in file1.readlines():

    if line.startswith(("22/11/2021")):

        print(line)

        file2.write(line)

file2.close()
file1.close()

I would like a way that it was possible to put a date range like (("01/01/2021" >>> "12/31/2021))

Thanks

  • Use regex for checking if it is date or not https://stackoverflow.com/questions/15491894/regex-to-validate-date-formats-dd-mm-yyyy-dd-mm-yyyy-dd-mm-yyyy-dd-mmm-yyyy – Arun Pal Nov 22 '21 at 17:25
  • Reformat the date into ISO 8601 format and you can then compare lexicographically. – dawg Nov 22 '21 at 17:27

0 Answers0