我有一个用python生成的动态列表,如何过滤该列表以仅显示带有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"
结果将是这些项目的列表
"PUBLIC_DAILY_201906130000_20190614040503.zip"
"PUBLIC_DAILY_201906150000_20190616040503.zip"
谢谢
答案 0 :(得分:3)
我们可以尝试在子字符串的帮助下进行过滤:
# list input
output = list(filter(lambda x: x[13:21] > '20190612', input))
print(output)
['PUBLIC_DAILY_201906150000_20190616040503.zip',
'PUBLIC_DAILY_201906130000_20190614040503.zip']
这实际上是第一个日期的子字符串,然后将其与另一个字符串日期进行比较。因为这里涉及的所有日期都是固定宽度,所以我们在这里进行了字符串比较。最佳做法可能是将所有文本字符串转换为真实日期,然后进行比较。
答案 1 :(得分:1)
尝试:
[i for i in lst if i > "PUBLIC_DAILY_20190612000"]
答案 2 :(得分:1)
您也可以尝试类似的方法。
通常最好先将字符串转换为日期。
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
答案 3 :(得分:1)
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)
打印:
['PUBLIC_DAILY_201906150000_20190616040503.zip', 'PUBLIC_DAILY_201906130000_20190614040503.zip']
答案 4 :(得分:1)
您只需将字符串切成薄片并将其转换为int,然后将其与您各自的日期进行比较 数据= [“ 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])