繁体   English   中英

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

[英]Most efficient way to sort array of MM/DD/YYYY strings in Python?

我正在尝试使用来自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

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

使用字符串切片和datetime.strptime

from datetime import datetime

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']

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']

用正则表达式?

import re

pattern = 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))]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM