python - 用日期时间Python替换字符串
问题描述
我想从此字符串替换从 到当前日期tmp
之间的以下字符。输出应该是这样的,
您能否就我如何做到这一点提供一些想法?谢谢!/tmp/
/tmp/tmpy2919ufy
/20200615/tmpy2919ufy
解决方案
你可以简单地用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'
推荐阅读
- python - EIA API:如何在代理后请求数据
- powershell - Powershell Invoke-RestMethod 未通过 PAT for Azure DevOps 进行身份验证
- wpf - 为什么我的 WPF 矩形控件没有填满 StackPanel 的所有空白空间?
- java - 如何从用户输入的 ArrayList 中获取一个值?
- python-3.x - 句子标记器检索跨度
- java - 将 JButton 添加到 JScrollPame
- wpf - 在 WPF 中结合文本换行和收缩
- c++ - 使用 FFmpeg API 的示例 C 代码中的问题
- ios - 如何防止移动 Chrome/Safari 中的对角滚动
- javascript - 匹配两个特殊字符之间的子字符串,不包括字符