신들은 조용한 개인 CSDN 블로그 게시물 디렉토리입니다.
이 기사에서는 Python3에서 처리 시간에 대한 다양한 라이브러리 및 사용 시나리오를 소개합니다.
마지막 업데이트 시간: 2023.6.2
가장 빠른 업데이트 시간: 2023.6.2
기사 디렉토리
1. 데이트타임 라이브러리
공식 문서: datetime — 기본 날짜 및 시간 유형 — Python 3.11.3 문서
datetime.datetime.now()
datetime.datetime.today()
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
2. 시간 라이브러리
time.time()
: 현재 시간을 반환(부동수 값)time.localtime(secs)
time.sleep(秒数)
: 수면 지정 시간
3. JioNLP 라이브러리: (중국어) 텍스트에서 시간 정보 추출
정규식으로 추출합니다.
설치 방법:pip install jionlp
시간 의미 분석 문서 · dongrixinyu/JioNLP Wiki · GitHub
온라인 데모: 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 객체 목록을 반환합니다.
5. 피츠 라이브러리
한 가지 말할 것은, 저는 실제로 이 라이브러리가 무엇을 하는지 이해하지 못합니다. 아마도 통일된 시간대 같은 것일까요?
어쨌든 설치하는 동안 설치해야 하는 다른 패키지가 있습니다.
피츠 PyPI