在最近的需求开发中,对包含时间的字段进行处理,提取出如下时间格式字符串'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'