output = []
with open(filename, 'r') as f:
for line in (l.strip() for l in f if l.strip()):
data = line.split()
output.append(line.replace(data[-1], datetime.strptime(data[-1],'%Y-%m-%d').strftime('%B %d, %Y')))
import time
text = """50 Cent 1975-07-06
75 Cents 1933-01-29
9th Wonder 1975-01-15
A Fine Frenzy 1984-12-23"""
for line in text.splitlines():
dob = line.rsplit(None, 1)[-1]
dob_new = time.strftime('%B %d, %Y', time.strptime(dob, '%Y-%m-%d'))
print line.replace(dob, dob_new)
一种方法是使用时间.strptime解析和时间.strftime格式化。
pydoc time
获取文档。在您可以使用strftime and strptime from datetime module 请在下面找到我的示例代码:
结果:
^{pr2}$奖金:
A strftime cheatsheet
相关问题 更多 >
编程相关推荐