Python怎么从年月日字符串中取出年份月份?

Python09

Python怎么从年月日字符串中取出年份月份?,第1张

切片或正则匹配。

切片:

year = 'YD210901-03'[2:4]

month = 'YD210901-03'[4:6]

正则匹配:

year, month = re.findall(r'YD(\d\d)(\d\d)\d\d-\d\d', 'YD210901-03')[0]

取得时间相关的信息的话,要用到python time模块,python time模块里面有很多非常好用的功能,你可以去官方文档了解下,要取的当前时间的话,要取得当前时间的时间戳,时间戳好像是1970年到现在时间相隔的时间。你可以试下下面的方式来取得当前时间的时间戳:import timeprint time.time()