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

谁都会走 提交于 2019-12-24 11:35:02

问题


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

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

Does anybody know how to reformat the list? Or perhaps earlier, reformatting the Series? Thank you.


回答1:


You don't even need to use datetime here. Just map those objects to a function that turns them into strings and splits them on the -, then use 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.




回答2:


You're almost there. The datetime.datetime.strptime converts a string into datetime object. You need datetime.datetime.strftime to convert it back to string:

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)



回答3:


You can solve it by a string point of view

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



回答4:


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]



回答5:


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'



回答6:


While it's not pretty, it should work to generate a month-day-year list.

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


来源:https://stackoverflow.com/questions/32258915/reformatting-a-list-of-date-strings-to-day-month-year-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!