问题
I am trying to figure out how to sort the record database given by their birthday. The original record has 501 names and data in it so I tried to get a chunk out of it and experiment some codes.
However, I couldn't really figure out how to sort these out. Here are the codes I tried to use.
import datetime
empRecords="""James,Butt,6649 N Blue Gum St,New Orleans,Orleans,LA,2/15/1956,70116,504-621-8927,504-845-1427,jbutt@gmail.com,http://www.bentonjohnbjr.com,;
Josephine,Darakjy,4 B Blue Ridge Blvd,Brighton,Livingston,MI,7/15/1988,48116,810-292-9388,810-374-9840,josephine_darakjy@darakjy.org,http://www.chanayjeffreyaesq.com,;
Art,Venere,8 W Cerritos Ave #54,Bridgeport,Gloucester,NJ,3/10/1988,8014,856-636-8749,856-264-4130,art@venere.org,http://www.chemeljameslcpa.com,;
Lenna,Paprocki,639 Main St,Anchorage,Anchorage,AK,9/11/1991,99501,907-385-4412,907-921-2010,lpaprocki@hotmail.com,http://www.feltzprintingservice.com,;
"""
emp = empRecords.split(";")
for i in range(len(emp)):
b= emp[i]
age = [datetime.datetime.strptime(ts, "%m/%d/%Y") for ts in b]
age.sort()
ages = [datetime.datetime.strftime(ts, "%m/%d/%Y") for ts in age]
print(ages)
Another one:
def get_bday(empl_record):
return datetime.strptime(empl_record[1], '%m/%d/%Y')
sorted(b, key=get_bday)
print(b)
回答1:
Your data is CSV, you should treat it as such. The date is in the 7th field of each row, we extract it first.
You don't have to convert to datetime object, sort, and convert back if you only want the string representation. Just use the datetime object as key for the sort.
So, using all that, with your original data in test.csv:
import csv
from datetime import datetime
with open('test.csv') as f:
reader = csv.reader(f)
dates = [row[6] for row in reader]
dates.sort(key=lambda ts: datetime.strptime(ts, "%m/%d/%Y"))
print(dates)
#['2/15/1956', '3/10/1988', '7/15/1988', '9/11/1991']
If you want the whole data sorted by date, you can adjust it to:
import csv
from datetime import datetime
with open('test.csv') as f:
reader = csv.reader(f)
rows = list(reader)
rows.sort(key=lambda row: datetime.strptime(row[6], "%m/%d/%Y"))
print(rows)
[['James', 'Butt', '6649 N Blue Gum St', 'New Orleans', 'Orleans', 'LA', '2/15/1956', '70116', '504-621-8927', '504-845-1427', 'jbutt@gmail.com', 'http://www.bentonjohnbjr.com', ';'],
['Art', 'Venere', '8 W Cerritos Ave #54', 'Bridgeport', 'Gloucester', 'NJ', '3/10/1988', '8014', '856-636-8749', '856-264-4130', 'art@venere.org', 'http://www.chemeljameslcpa.com', ';'],
['Josephine', 'Darakjy', '4 B Blue Ridge Blvd', 'Brighton', 'Livingston', 'MI', '7/15/1988', '48116', '810-292-9388', '810-374-9840', 'josephine_darakjy@darakjy.org', 'http://www.chanayjeffreyaesq.com', ';'],
['Lenna', 'Paprocki', '639 Main St', 'Anchorage', 'Anchorage', 'AK', '9/11/1991', '99501', '907-385-4412', '907-921-2010', 'lpaprocki@hotmail.com', 'http://www.feltzprintingservice.com', ';']]
Contents of test.csv:
James,Butt,6649 N Blue Gum St,New Orleans,Orleans,LA,2/15/1956,70116,504-621-8927,504-845-1427,jbutt@gmail.com,http://www.bentonjohnbjr.com,;
Josephine,Darakjy,4 B Blue Ridge Blvd,Brighton,Livingston,MI,7/15/1988,48116,810-292-9388,810-374-9840,josephine_darakjy@darakjy.org,http://www.chanayjeffreyaesq.com,;
Art,Venere,8 W Cerritos Ave #54,Bridgeport,Gloucester,NJ,3/10/1988,8014,856-636-8749,856-264-4130,art@venere.org,http://www.chemeljameslcpa.com,;
Lenna,Paprocki,639 Main St,Anchorage,Anchorage,AK,9/11/1991,99501,907-385-4412,907-921-2010,lpaprocki@hotmail.com,http://www.feltzprintingservice.com,;
回答2:
Try this,
age = [datetime.datetime.strptime(ts.split(",")[6], "%m/%d/%Y") for ts in emp if ts.strip()]
age.sort()
ages = [datetime.datetime.strftime(ts, "%m/%d/%Y") for ts in age]
来源:https://stackoverflow.com/questions/61848097/time-data-n-does-not-match-format-m-d-y-python