how to filter a list of string that contains dates

Viewed 1128

I have a dynamic list generated in python, How to filter that list to show only the files with dates > 20190612

"PUBLIC_DAILY_201906150000_20190616040503.zip"
"PUBLIC_DAILY_201906110000_20190612040501.zip"
"PUBLIC_DAILY_201906120000_20190613040503.zip"
"PUBLIC_DAILY_201906100000_20190611040501.zip"
"PUBLIC_DAILY_201906130000_20190614040503.zip"

The result will be a list of those items

"PUBLIC_DAILY_201906130000_20190614040503.zip"
"PUBLIC_DAILY_201906150000_20190616040503.zip"

Thank You

5 Answers

We can try filtering with the help of substring:

# list input
output = list(filter(lambda x: x[13:21] > '20190612', input))
print(output)

['PUBLIC_DAILY_201906150000_20190616040503.zip',
 'PUBLIC_DAILY_201906130000_20190614040503.zip']

This actually take a substring of the first date, and then compares it against another string date. We get away with doing a string comparison here because all dates involved are fixed width. Best practice though might be to convert all text strings to bona fide dates and then compare.

Try:

[i for i in lst if i > "PUBLIC_DAILY_20190612000"]

You could also try something like this out.

Its usually better to convert the strings to dates first.

archives = ["PUBLIC_DAILY_201906150000_20190616040503.zip",
"PUBLIC_DAILY_201906110000_20190612040501.zip",
"PUBLIC_DAILY_201906120000_20190613040503.zip",
"PUBLIC_DAILY_201906100000_20190611040501.zip",
"PUBLIC_DAILY_201906130000_20190614040503.zip"]

date_regex = "%Y%m%d%H%M%S"
default_regex = "%Y%m%d"
default_date = "20190612"
compare_date = datetime.datetime.strptime(default_date,default_regex)
passed_list = []

for archive in archives:
    split_str = archive.split("_")
    print split_str

    date1 = datetime.datetime.strptime(split_str[2],date_regex)
    date2 = datetime.datetime.strptime(split_str[3].split(".")[0],date_regex)
    if date1 > compare_date and date2 > compare_date:
        passed_list.append(archive)

print passed_list
import datetime
import re

lst = ["PUBLIC_DAILY_201906150000_20190616040503.zip",
"PUBLIC_DAILY_201906110000_20190612040501.zip",
"PUBLIC_DAILY_201906120000_20190613040503.zip",
"PUBLIC_DAILY_201906100000_20190611040501.zip",
"PUBLIC_DAILY_201906130000_20190614040503.zip"]

d = datetime.datetime(year=2019, month=6, day=12)

l = [l for l in lst for g in re.findall(r'PUBLIC_DAILY_(\d{4})(\d{2})(\d{2})', l) if datetime.datetime.strptime(''.join(g), '%Y%m%d') > d]
print(l)

Prints:

['PUBLIC_DAILY_201906150000_20190616040503.zip', 'PUBLIC_DAILY_201906130000_20190614040503.zip']

you can simply slice the string and convert it into int and then compare it with your respective date data = ["PUBLIC_DAILY_201906150000_20190616040503.zip", "PUBLIC_DAILY_201906110000_20190612040501.zip", "PUBLIC_DAILY_201906120000_20190613040503.zip", "PUBLIC_DAILY_201906100000_20190611040501.zip", "PUBLIC_DAILY_201906130000_20190614040503.zip"]

for i in range(len(data)):
    date=int(data[i][13:21])
    if date>20190612:
    print(data[i])
Related