Python3中对时间的处理(持续更新ing...)

诸神缄默不语-个人CSDN博文目录

本文介绍Python3中各种处理时间的库和使用方案

1. datetime库

官方文档:datetime — Basic date and time types — Python 3.11.3 documentation

  1. datetime.datetime.now()
  2. datetime.datetime.today()
  3. datetime.datetime.strptime(字符串,"%Y-%m-%d")(第二个入参是时间格式)
    代码示例,计算两个日期之间相隔的天数和月数:
    import datetime
    def days(str1,str2):
        date1=datetime.datetime.strptime(str1,"%Y-%m-%d")
        date2=datetime.datetime.strptime(str2,"%Y-%m-%d")
        num=(date1-date2).days
        return num
     
    def months(str1,str2):
        year1=datetime.datetime.strptime(str1,"%Y-%m-%d").year
        year2=datetime.datetime.strptime(str2,"%Y-%m-%d").year
        month1=datetime.datetime.strptime(str1,"%Y-%m-%d").month
        month2=datetime.datetime.strptime(str2,"%Y-%m-%d").month
        num=(year1-year2)*12+(month1-month2)
        return num
    
    print(days('2023-5-18','2023-5-10'))
    print(days('2023-5-18','2021-5-10'))
    
    输出:
    8
    738
    
    1. 入参格式,还可以这样:
      %Y-%m-%dT%H:%M:%SZ
  4. datetime对象
    1. 转换成date对象:dt.strftime("%Y-%m-%d") / dt.date()

2. time库

  1. time.time():返回当前时间(float值)
  2. time.localtime(secs)
  3. time.sleep(秒数):睡眠指定时间

3. JioNLP库:(中文)从文本中提取时间信息

通过正则表达式来提取的。

安装方式:pip install jionlp

时间语义解析 说明文档 · dongrixinyu/JioNLP Wiki · GitHub
在线demo:http://www.jionlp.com/jionlp_online/extract_time

示例代码:

import time
import jionlp as jio
time_text_list = ['2021年前两个季度', '从2018年12月九号到十五号', '2019年感恩节', '每周六上午9点到11点', '30~90日']
for time_text in time_text_list:
    print(jio.parse_time(time_text, time_base=time.time()))

输出:

# jionlp - 微信公众号: JioNLP  Github: `https://github.com/dongrixinyu/JioNLP`.
# jiojio - `http://www.jionlp.com/jionlp_online/cws_pos` is available for online trial.
# jiojio - Successfully load C funcs for CWS and POS acceleration.
{'type': 'time_span', 'definition': 'accurate', 'time': ['2021-01-01 00:00:00', '2021-06-30 23:59:59']}
{'type': 'time_span', 'definition': 'accurate', 'time': ['2018-12-09 00:00:00', '2018-12-15 23:59:59']}
{'type': 'time_point', 'definition': 'accurate', 'time': ['2019-11-28 00:00:00', '2019-11-28 23:59:59']}
{'type': 'time_period', 'definition': 'accurate', 'time': {'delta': {'day': 7}, 'point': {'time': ['2023-06-03 09:00:00', '2023-06-03 11:00:00'], 'string': '周六上午9点到11点'}}}
{'type': 'time_delta', 'definition': 'blur', 'time': [{'day': 30.0}, {'day': 90.0}]}

可以看文档,time_span大概指时间段(头尾都是具体时间),time_point是时间点,time_period是时间周期,time_delta是时间长度。

4. datefinder库:(英文)从文本中提取时间信息

pypi网址:datefinder · PyPI
pip安装方式:pip install datefinder

示例代码:

dt=list(datefinder.find_dates("ASPLOS会议的官网是https://www.asplos-conference.org/,2023年论文提交截止时间是2022年10月20日。"))

返回一个由datetime对象组成的list。

5. pytz库

有一说一,我其实没看懂这个库在干啥,大概是统一时区之类的?
反正有一些别的包在安装时会顺便要求装上的。
pytz · PyPI

6. python-duckling

还没用过,先列出

https://github.com/FraBle/python-duckling
Python常见实体提取库Duckling,多语言,实体如日期、金额、距离_duckling python-CSDN博客

↑是这个包的Python包:https://github.com/facebookarchive/duckling_old

↑这个包的新版:https://github.com/facebook/duckling

↑这个包的Python包:https://github.com/duckling-python-wrapper/fb_duckling

(反正就是这个更新迭代史已经扭成一股麻花了……)

7. Time-NLP

还没用过,先列出

https://github.com/zhanzecheng/Time_NLP
https://pypi.org/project/ChineseTimeNLP/

↑2个都是这个包的Python包:https://github.com/bububa/TimeNLP

8. WeTextProcessing

还没用过,先列出

https://github.com/wenet-e2e/WeTextProcessing

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

诸神缄默不语

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值