时间数据'\ n'与格式'%m/%d/%Y'不匹配-python [英] Time data '\n' does not match format '%m/%d/%Y' - python

查看:64
本文介绍了时间数据'\ n'与格式'%m/%d/%Y'不匹配-python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何对他们生日那天给出的记录数据库进行排序.原始记录中包含501个名称和数据,因此我尝试从中获取一部分并尝试一些代码.

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)

另一个:


def get_bday(empl_record):
    return datetime.strptime(empl_record[1], '%m/%d/%Y')

sorted(b, key=get_bday)
print(b)

推荐答案

您的数据为CSV,因此应将其视为CSV.日期在每行的第7个字段中,我们首先将其提取.

Your data is CSV, you should treat it as such. The date is in the 7th field of each row, we extract it first.

如果只需要字符串表示形式,则不必转换为datetime对象,进行排序和转换回去.只需将datetime对象用作排序的键即可.

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.

因此,将所有内容与原始数据一起存储在 test.csv 中:

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

test.csv 的内容:

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,;

这篇关于时间数据'\ n'与格式'%m/%d/%Y'不匹配-python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆