在python中按日期和字符串排序 [英] Sort by date and string in python
问题描述
我有一堆文件名为
companyname-date_somenumber.txt
companyname-date_somenumber.txt
我必须根据公司名称对文件进行排序,然后根据日期对文件进行排序,然后按此排序顺序将其内容复制到另一个文本文件中.
I have to sort the files according to company name, then according to date, and copy their content in this sorted order to another text file.
这是我正在尝试的方法:
Here's the approach I'm trying :
从每个文件名中提取公司名称,然后提取日期,将这两个字段放入字典中,将此字典追加到列表中,然后根据公司名称的两个列对日期进行排序.
From each file-name, extract company name and then date, put these two fields in a dictionary, append this dictionary to a list and then sort this list according to the two columns of companyname and then date.
然后,一旦我获得了排序顺序,我想我可以根据刚刚获得的文件顺序在文件夹中搜索文件,然后将每个文件的内容复制到txt文件中,然后得到最终的txt文件
Then once I have the sorted order, I think I could search for the files in the folder according to the file-order I just obtained, then copy each files content into a txt file and I'll have my final txt file.
这是我到目前为止的代码:
Here's the code I have so far :
myfiles = [ f for f in listdir(path) if isfile(join(path,f)) ]
file_list=[]
for file1 in myfiles:
# find indices of companyname and date in the file-name
idx1=file1.index('-',0)
idx2=file1.index('_',idx1)
company=file1[0:idx1] # extract companyname
thisdate=file1[idx1+1:idx2] #extract date, which is in format MMDDYY
dict={}
# extract month, date and year from thisdate
m=thisdate[0:2]
d=thisdate[2:4]
y='20'+thisdate[4:6]
# convert into date object
mydate = date(int(y), int(m), int(d))
dict['date']=mydate
dict['company']=company
file_list.append(dict)
我在此代码块的末尾检查了file_list的输出,我认为我有字典列表.现在,如何按公司名称和日期排序?我在网上查找了按多个键排序的信息,但是如何按日期获得递增的顺序?
I checked the output of file_list at the end of this block of code and I think I have my list of dicts. Now, how do I sort by companyname and then by date? I looked up sorting by multiple keys online but how would I get the increasing order by date?
还有其他方法可以按字符串对列表进行排序,然后对日期字段进行排序吗?
Is there any other way that I could sort a list by a string and then a date field?
推荐答案
import os
from datetime import datetime
MY_DIR = 'somedirectory'
# my_files = [ f for f in os.listdir(MY_DIR) if os.path.isfile(os.path.join(MY_DIR,f)) ]
my_files = [
'ABC-031814_01.txt',
'ABC-031214_02.txt',
'DEF-010114_03.txt'
]
file_list = []
for file_name in my_files:
company,_,rhs = file_name.partition('-')
datestr,_,rhs = rhs.partition('_')
file_date = datetime.strptime(datestr,'%m%d%y')
file_list.append(dict(file_date=file_date,file_name=file_name,company=company))
for row in sorted(file_list,key=lambda x: (x.get('company'),x.get('file_date'))):
print row
函数sorted
带有关键字参数key
,该参数是应用于要排序的序列中每个项目的函数.如果此函数返回一个元组,则该序列将按元组中的项目依次排序.
The function sorted
takes a keyword argument key
that is a function applied to each item in the sequence you're sorting. If this function returns a tuple, the sequence will be sorted by the items in the tuple in turn.
此处lambda x: (x.get('company'),x.get('file_date'))
允许sorted
按公司名称排序,然后按日期排序.
Here lambda x: (x.get('company'),x.get('file_date'))
allows sorted
to sort by company name and then by date.
这篇关于在python中按日期和字符串排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!