Python 多个日期的字符串转换为日期格式应该怎么设置(datetime模块)

如果是1个固定的内容,代码应该是可以的,但是要求是用text所有内容,请问如何实现呢?或者前后哪里有错吗?谢谢!
import re
import datetime

text='''
    1.   abc  2016-10-31  
    2.   xyz   2016-9-4
    3.   aef   2016-10-aa
    4.    asasf asdf 10-14
    5.  2013-10-3  234234
    6.  1945-8-15 abc  1945
    7.  1972-01-30 asdf  1988-10-1
'''

a = re.findall(r'\d{4}-\d{1,2}-\d{1,2}',text)
b = ",".join(a)
c = datetime.datetime.strptime(b,'%Y-%m-%d').strftime('%m/%d/%Y')

print(a.index(i)+1,end='. ')
print(c)

把b=','.join(a)删掉,然后print代码放在循环里

a = re.findall(r'\d{4}-\d{1,2}-\d{1,2}',text)
#b = ",".join(a)
for i in a:
    print(a.index(i) + 1, end='. ')
    print(datetime.datetime.strptime(i,'%Y-%m-%d').strftime('%m/%d/%Y'))

因为re.findall方法返回的是一个列表,需要用for循环遍历列表(a)的每一个元素才能得到日期文本。

import re
import datetime

text='''
    1.   abc  2016-10-31  
    2.   xyz   2016-9-4
    3.   aef   2016-10-aa
    4.    asasf asdf 10-14
    5.  2013-10-3  234234
    6.  1945-8-15 abc  1945
    7.  1972-01-30 asdf  1988-10-1
'''
 
dates = re.findall(r'\d{4}-\d{1,2}-\d{1,2}',text)


for d in dates:
        text = text.replace(d,datetime.datetime.strptime(d,'%Y-%m-%d').strftime('%m/%d/%Y'))

print(text)
 
#输出结果:
'''
    1.   abc  10/31/2016  
    2.   xyz   09/04/2016
    3.   aef   2016-10-aa
    4.    asasf asdf 10-14
    5.  10/03/2013  234234
    6.  08/15/1945 abc  1945
    7.  01/30/1972 asdf  10/01/1988
'''

或者:

import re
import datetime

text='''
    1.   abc  2016-10-31  
    2.   xyz   2016-9-4
    3.   aef   2016-10-aa
    4.    asasf asdf 10-14
    5.  2013-10-3  234234
    6.  1945-8-15 abc  1945
    7.  1972-01-30 asdf  1988-10-1
'''
 
text =  re.sub(r'\d{4}-\d{1,2}-\d{1,2}',lambda d:datetime.datetime.strptime(d.group(0),'%Y-%m-%d').strftime('%m/%d/%Y'),text)

print(text)
 
#输出结果:
'''
    1.   abc  10/31/2016  
    2.   xyz   09/04/2016
    3.   aef   2016-10-aa
    4.    asasf asdf 10-14
    5.  10/03/2013  234234
    6.  08/15/1945 abc  1945
    7.  01/30/1972 asdf  10/01/1988
'''