[Work Summary] 利用Python进行时间格式字符串转换
2023-09-11 14:22:53 时间
![](https://img-blog.csdnimg.cn/0e25dfa5e09341a99f2792cf253d7479.png)
在最近的需求开发中,对包含时间的字段进行处理,提取出如下时间格式字符串'2022/09/05',为了便于后续的操作计算,需要将其转换成'2022/9/5' (即'2022/09/05' => '2022/9/5'),具体Python代码如下所示:
def deal_time(new_time):
"""
时间格式字符串转换函数
举例:2022/09/05 => 2022/9/5
"""
deal_new_time = new_time.split('/')
year = deal_new_time[0]
month = deal_new_time[1]
month = str(int(month))
day = deal_new_time[2]
day = str(int(day))
str_list = [year, month, day]
res = '/'.join(str_list)
return res
deal_time('2022/09/05') # '2022/9/5'
deal_time('2022/09/10') # '2022/9/10'
deal_time('2022/12/05') # '2022/12/5'
相关文章
- How to run python interactive in current file's directory in Visual Studio Code? Python路径问题
- Python将图片转换为ASCII字符画
- Python kafka操作实例(kafka-python)
- Word处理控件Aspose.Words功能演示:在 Python 中将 Word 文档转换为 PNG、JPEG 或 BMP
- 【华为OD机试真题 python】投篮大赛【2022 Q4 | 100分】
- 【零基础学python】:清华官方出品的《看漫画学Python》全彩PDF,495页资源分享
- 如何快速学习python,学好python?能通过Python赚到的第一笔钱,有哪些经验可以分享吗?
- python nmap网络扫描详解
- gyp ERR! stack Error: Can‘t find Python executable “python“, you can set the PYTHON env variable.
- Python中的字典
- python 将png图片格式转换生成gif动画
- Python 安装环境
- python中stack在实际中的简单应用之进制转换
- python之模块py_compile用法(将py文件转换为pyc文件)
- python 调用API时异常捕获的技巧
- Python 三国人物数据快速采集极简方法汇总
- Python 实现被动收入教程之我如何使用 python 制作我的第一个高级telegram机器人
- 【python】or【php】网页中字符编码转换,将反斜杠u u字符串转为unicode/utf8
- 华为OD机试 - 匿名信(Python)| 真题+思路+考点+代码+岗位
- python pdf 文件,流转换
- Python矩阵操作:维度变换、矩阵组合、矩阵分割