[英]Converting a list of dates in string to Month, Day, Year in Python
I have a list of dates in a string that looks like this: 我在一个看起来像这样的字符串中有一个日期列表:
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']
I want it to be formatted like this: 我希望它的格式如下:
Date_List
Out[83]:
['08-24-2015',
'08-30-2015',
'08-22-2015',
'08-21-2015',
'08-25-2015',
'08-29-2015']
I tried Date_List = ['{}-{}-{}'.format(m,d,y) for y, m, d in map(lambda x: str(x).split('-'), Date_List)]
我在地图中尝试了Date_List = ['{}-{}-{}'.format(m,d,y) for y, m, d in map(lambda x: str(x).split('-'), Date_List)]
This returns 这返回
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']
Anybody know how to convert and ignore the 00:00:00 任何人都知道如何转换而忽略了00:00:00
I also tried 我也试过
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)
but this outputs a generator object? 但这会输出一个生成器对象?
Date_List
Out[91]: <generator object <genexpr> at 0x2047A5A8>
Which means if I run the code I get this error: TypeError: <generator object <genexpr> at 0x1FBCFC38> is not JSON serializable
这意味着如果我运行代码,将出现以下错误: TypeError: <generator object <genexpr> at 0x1FBCFC38> is not JSON serializable
You're very close; 你很亲近 you just need to use a list comprehension on the last line instead of a generator expression. 您只需要在最后一行使用列表推导,而不要使用生成器表达式。
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]
I would clean it up like so: 我会像这样清理它:
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)
Output: 输出:
['08-24-2015',
'08-30-2015',
'08-22-2015',
'08-21-2015',
'08-25-2015',
'08-29-2015']
Here's a slightly more generalized way to do it: 这是一种稍微通用的方法:
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)
Output: 输出:
['08-24-2015',
'08-30-2015',
'08-22-2015',
'08-21-2015',
'08-25-2015',
'08-29-2015']
EDIT: Order fixed. 编辑:定单。
EDIT2: Zero padding fixed after Paulo's suggestion 编辑2:保罗的建议后固定零填充
Try: 尝试:
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
)
)
or 要么
["{0:02}-{1:02}-{2}".format(d.month, d.day, d.year) for d in map(lambda d: parser.parse(d), dates)]
This should do the trick: 这应该可以解决问题:
['{}-{}-{}'.format(m,d,y) for y, m, d in map(lambda x: x.split()[0].split('-'), Date_List)]
You don't need str(x)
because it's already a string. 您不需要str(x)
因为它已经是一个字符串。 You then split()
the string, which by default splits on whitespace, and take the first part ( [0]
). 然后,您对字符串进行split()
该字符串默认情况下在空格上分割,并采用第一部分( [0]
)。 You then split('-')
on the hyphens. 然后,在连字符上split('-')
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.