猿问

在 Python 中对 MM/DD/YYYY 字符串数组进行排序的最有效方法?

我正在尝试使用来自https://senatestockwatcher.com/的数据,特别是获取最新文件。根据 API 页面,这需要获取 Amazon S3 存储桶中的文件列表,然后找到最新的并获取它。


我目前使用的代码是:


data = requests.get(url).text

data = xmltodict.parse(data)

data = json.loads(json.dumps(data))

data = data["ListBucketResult"]["Contents"]

filenames = [item["Key"] for item in data if "data/" in item["Key"]][1:]

filenames.sort()

print(filenames)

但是,我遇到的问题是文件名的格式为:


transaction_report_for_01_02_2013.json

transaction_report_for_01_03_2017.json


对数组使用常规 python.sort()函数不起作用,因为它从左到右读取名称字符串,因此忽略了年份。将这些文件从最新到最旧准确排序的最有效方法是什么?


守候你守候我
浏览 164回答 2
2回答

白猪掌柜的

使用字符串切片和datetime.strptime:from datetime import datetimetransactions = ['transaction_report_for_01_02_2013.json', 'transaction_report_for_01_03_2017.json','transaction_report_for_08_03_2015.json','transaction_report_for_09_03_2015.json']def custom_sort(filename):  # assuming a constant string end length slice the date and parse it  return datetime.strptime(filename[-15:-5], '%d_%m_%Y')print(transactions)#['transaction_report_for_01_02_2013.json', 'transaction_report_for_01_03_2017.json', 'transaction_report_for_08_03_2015.json', 'transaction_report_for_09_03_2015.json']transactions.sort(key=custom_sort)print(transactions)#['transaction_report_for_01_02_2013.json', 'transaction_report_for_08_03_2015.json', 'transaction_report_for_09_03_2015.json', 'transaction_report_for_01_03_2017.json']

ABOUTYOU

用正则表达式?import repattern = re.compile(r'^.*(\d{2})_(\d{2})_(\d{4}).*$')keys    = [x.match.group(3)+x.match.group(1)+x.match.group(2)           for x in filenames           ]filenames = [y for x,y in sorted(zip(keys,filenames))]
随时随地看视频慕课网APP

相关分类

Go
我要回答