繁体   English   中英

在Python中将字符串中的日期列表转换为月,日,年

[英]Converting a list of dates in string to Month, Day, Year in Python

我在一个看起来像这样的字符串中有一个日期列表:

Date_List
Out[83]: 
['2015-08-24 00:00:00',
 '2015-08-30 00:00:00',
 '2015-08-22 00:00:00',
 '2015-08-21 00:00:00',
 '2015-08-25 00:00:00',
 '2015-08-29 00:00:00']

我希望它的格式如下:

Date_List
Out[83]: 
['08-24-2015',
 '08-30-2015',
 '08-22-2015',
 '08-21-2015',
 '08-25-2015',
 '08-29-2015']

我在地图中尝试了Date_List = ['{}-{}-{}'.format(m,d,y) for y, m, d in map(lambda x: str(x).split('-'), Date_List)]

这返回

Date_List
Out[85]: 
['08-24 00:00:00-2015',
 '08-30 00:00:00-2015',
 '08-22 00:00:00-2015',
 '08-21 00:00:00-2015',
 '08-25 00:00:00-2015',
 '08-29 00:00:00-2015']

任何人都知道如何转换而忽略了00:00:00

我也试过

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

但这会输出一个生成器对象?

Date_List
Out[91]: <generator object <genexpr> at 0x2047A5A8>

这意味着如果我运行代码,将出现以下错误: TypeError: <generator object <genexpr> at 0x1FBCFC38> is not JSON serializable

你很亲近 您只需要在最后一行使用列表推导,而不要使用生成器表达式。

Date_List = (datetime.datetime.strptime(i, "%Y-%m-%d %H:%M:%S") for i in Date_List)
Date_List = [datetime.datetime.strftime(i, "%m-%d-%Y") for i in Date_List]

我会像这样清理它:

from datetime import datetime
from pprint import pprint

timestamps = [
    '2015-08-24 00:00:00',
    '2015-08-30 00:00:00',
    '2015-08-22 00:00:00',
    '2015-08-21 00:00:00',
    '2015-08-25 00:00:00',
    '2015-08-29 00:00:00',
    ]

dates = (datetime.strptime(ts, '%Y-%m-%d %H:%M:%S') for ts in timestamps)
date_strings = [datetime.strftime(d, '%m-%d-%Y') for d in dates]

pprint(date_strings)

输出:

['08-24-2015',
 '08-30-2015',
 '08-22-2015',
 '08-21-2015',
 '08-25-2015',
 '08-29-2015']

这是一种稍微通用的方法:

from datetime import datetime
from pprint import pprint


def convert_timestamp(ts, from_pattern, to_pattern):
    dt = datetime.strptime(ts, from_pattern)
    return datetime.strftime(dt, to_pattern)


timestamps = [
    '2015-08-24 00:00:00',
    '2015-08-30 00:00:00',
    '2015-08-22 00:00:00',
    '2015-08-21 00:00:00',
    '2015-08-25 00:00:00',
    '2015-08-29 00:00:00',
    ]

date_strings = [convert_timestamp(ts, '%Y-%m-%d %H:%M:%S', '%m-%d-%Y')
                for ts in timestamps]

pprint(date_strings)

输出:

['08-24-2015',
 '08-30-2015',
 '08-22-2015',
 '08-21-2015',
 '08-25-2015',
 '08-29-2015']

编辑:定单。

编辑2:保罗的建议后固定零填充

尝试:

from dateutil import parser
map(
    lambda d: "{0:02}-{1:02}-{2}".format(d.month, d.day, d.year),
    map(
        lambda d: parser.parse(d),
        dates
    )
)

要么

["{0:02}-{1:02}-{2}".format(d.month, d.day, d.year) for d in map(lambda d: parser.parse(d), dates)]

这应该可以解决问题:

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

您不需要str(x)因为它已经是一个字符串。 然后,您对字符串进行split()该字符串默认情况下在空格上分割,并采用第一部分( [0] )。 然后,在连字符上split('-')

暂无
暂无

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

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