我当前的数组如下:
print(alldates)
其输出:
array([datetime.date(2019, 1, 25), datetime.date(2019, 1, 26),
datetime.date(2019, 1, 27), datetime.date(2019, 1, 29),
datetime.date(2019, 1, 31), datetime.date(2019, 2, 1)], dtype=object)
我想将其转换为如下形式:
alldates = ['2019-01-25'.....,'2019-02-01']
答案 0 :(得分:2)
使用.astype(str)
:
print(alldates.astype(str))
哪个输出:
['2019-01-25' '2019-01-26' '2019-01-27' '2019-01-29' '2019-01-31'
'2019-02-01']
答案 1 :(得分:0)
您可以使用str()传输到字符串,然后追加到新数组
import datetime
alldates=[datetime.date(2019, 1, 25), datetime.date(2019, 1, 26),datetime.date(2019, 1, 27), datetime.date(2019, 1, 29),datetime.date(2019, 1, 31), datetime.date(2019, 2, 1)]
new_allldates = []
for item in alldates:
new_allldates.append(str(item))
print(new_allldates)
结果:
['2019-01-25','2019-01-26','2019-01-27','2019-01-29','2019-01-31','2019-02-01 ']
答案 2 :(得分:0)
您可以使用以下代码来获得结果,
datelist = [datetime.date(2019, 1, 25), datetime.date(2019, 1, 26),datetime.date(2019, 1, 27), datetime.date(2019, 1, 29),datetime.date(2019, 1, 31), datetime.date(2019, 2, 1)]
resultlist = [i.strftime('%d-%m-%Y') for i in datelist]
print(resultlist)
结果:
['25-01-2019','26 -01-2019','27 -01-2019','29 -01-2019','31 -01-2019','01 -02-2019 ']