简体   繁体   English

在Python中将日期字符串列表重新格式化为日,月,年

[英]Reformatting a list of date strings to day, month, year in Python

I want to change the format of strings in a list. 我想更改列表中字符串的格式。 They are dates but have been converted into strings. 它们是日期,但已转换为字符串。

I have looked all over for this solution, but there seem to only be answers for a single element. 我到处都在寻找这种解决方案,但是似乎只有一个要素可以解决。

First I make a series 首先我做一个系列

Date_List = df.loc['receiveddate']
print Date_List

Element1                               2015-06-26
Element2                               2015-06-25
Element3                               2015-06-26
Element4                               2015-06-25
Element5                               2015-06-25
Element6                               2015-07-01

Then I convert it into a list. 然后,我将其转换为列表。

Date_List = [str(i) for i in Date_List]

which gives me 这给了我

['2015-06-26', '2015-06-25', '2015-06-26', '2015-06-25', '2015-06-25', '2015-07-01']

Instead, I want a list of strings that go day, month, year 相反,我要列出每天,每月,每年的字符串列表

['06-26-2015', '06-25-2015...etc.]

I've found that the most common suggestion is to use strftime. 我发现最常见的建议是使用strftime。 Trying Date_List = (datetime.datetime.strptime(i, "%B %d-%Y") for i in Date_List) Just gave me ValueError: time data '2015-06-26' does not match format '%B %d-%Y' Date_List = (datetime.datetime.strptime(i, "%B %d-%Y") for i in Date_List)尝试Date_List = (datetime.datetime.strptime(i, "%B %d-%Y") for i in Date_List)给了我ValueError: time data '2015-06-26' does not match format '%B %d-%Y'

So I tried 所以我尝试了

Date_List = (datetime.datetime.strptime(i, "%Y-%m-%d") for i in Date_List)

This returned ['2015-06-26 00:00:00', '2015-06-25 00:00:00', '2015-06-26 00:00:00', '2015-06-25 00:00:00', '2015-06-25 00:00:00', '2015-07-01 00:00:00'] 这返回了['2015-06-26 00:00:00', '2015-06-25 00:00:00', '2015-06-26 00:00:00', '2015-06-25 00:00:00', '2015-06-25 00:00:00', '2015-07-01 00:00:00']

Does anybody know how to reformat the list? 有人知道如何重新格式化列表吗? Or perhaps earlier, reformatting the Series? 还是更早些,重新格式化系列赛? Thank you. 谢谢。

You're almost there. 你快到了。 The datetime.datetime.strptime converts a string into datetime object. datetime.datetime.strptime将字符串转换为datetime对象。 You need datetime.datetime.strftime to convert it back to string: 您需要datetime.datetime.strftime才能将其转换回字符串:

Date_List = (datetime.datetime.strptime(i, "%Y-%m-%d") for i in Date_List)
Date_List = (datetime.datetime.strftime(i, "%m-%d-%Y") for i in Date_List)

You don't even need to use datetime here. 您甚至不需要在这里使用datetime Just map those objects to a function that turns them into strings and splits them on the - , then use str.format() : 只需将这些对象映射到将其转换为字符串并在-上拆分它们的函数,然后使用str.format()

Date_List = ['{}-{}-{}'.format(m,d,y) for y, m, d in map(lambda x: str(x).split('-'), Date_List)]

Also, from your code snippet it looks like you want the month first, not the day. 另外,从您的代码段中,您看起来像是要先月,而不是日。

您可以从字符串的角度解决它

New_Date_List = [d[8:10]+"-"+d[5:7]+"-"+d[:4] for d in Date_List]

Change your comprehension to 将您的理解更改为

import datetime
Date_List = [datetime.datetime.strptime(str(i), '%Y-%m-%d').strftime('%m-%d-%Y') for i in Date_List]

Regular expressions provide a pretty straightforward solution: 正则表达式提供了一个非常简单的解决方案:

>>> import re
>>> re.sub(r'(\d{4})-(\d{2})-(\d{2})', r'\2-\3-\1', '2015-06-25')
'06-25-2015'

虽然不是很漂亮,但是应该可以生成月日日列表。

New_Date_List = [[d][5:7] + '-' + Date_List[d][-2:] + '-' + Date_List[d][:4] for d in Date_List]

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

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