Python日期时间通用匹配工具dateutil,匹配中文
dateutil不能直接匹配中文,这里的策略是把日期时间中的中文字符删掉,合并成一条连续的数字字符串,然后交给dateutil匹配。
from pprint import pprint
import dateutil.parser as psdef remove_chinese(char):# 检测是否是中文字符if 'u4e00' <= char <= 'u9fa5':return ''return charif __name__ == '__main__':d1 = ps.parse('20220712195859')pprint(d1.strftime('%Y-%m-%d %H:%M:%S'))d2 = ps.parse('2022-07-2n')pprint(d2.strftime('%Y-%m-%d %H:%M:%S'))d3 = ps.parse('5:09:12')pprint(d3.strftime('%Y-%m-%d %H:%M:%S'))d4 = ps.parse('7-11 23:19n')pprint(d4.strftime('%Y-%m-%d %H:%M:%S'))d5 = ps.parse('2022/07/13 12:32')pprint(d5.strftime('%Y-%m-%d %H:%M:%S'))try:s6 = '2022年07月11日23时19分n'lst = filter(lambda x: remove_chinese(x), s6) # 如果是中文字符则删掉d6 = ps.parse(''.join(lst))pprint(d6.strftime('%Y-%m-%d %H:%M:%S'))except:pprint('错误')pass
输出:
'2022-07-12 19:58:59'
'2022-07-02 00:00:00'
'2022-07-21 05:09:12'
'2022-07-11 23:19:00'
'2022-07-13 12:32:00'
'2022-07-11 23:19:00'
本文发布于:2024-01-28 09:09:38,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/17064041836338.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |