首页 > 解决方案 > 用日期时间Python替换字符串

问题描述

我想从此字符串替换从 到当前日期tmp之间的以下字符。输出应该是这样的, 您能否就我如何做到这一点提供一些想法?谢谢!/tmp//tmp/tmpy2919ufy/20200615/tmpy2919ufy

标签: pythonstringdatetimereplace

解决方案


你可以简单地用datetime做到这一点

from datetime import datetime

string = "/tmp/tmpy2919ufy"
replace_string = datetime.today().strftime('%Y%m%d')

string = string.replace('tmp',replace_string,1)

#output >>> '/20200615/tmpy2919ufy' 


推荐阅读