gggdtparser
通用时间解析器(General General General DateTime Parser) ,是基于Python编写的字符串时间抽取解析工具,经过了众多测试用例进行测试,具有通用、高效、准确的解析能力。
项目背景
- 在日常开发中,特别是爬虫采集时,来自各种语言、各种格式、语义化话的字符串时间,对其解析非常复杂繁琐。
- 通用的字符串解析需要考虑的情况很多,既要尽可能多地满足各类字符串时间格式,还需对解析出的结果的准确有很高要求。
- Python中各类解析库,在解析前的要求很高,只能解析无噪声的字符串时间,极为不方便。
项目原理
- 采用正则的方式完成对时间的抽取,进一步对抽取到的时间进行解析,即抽取和解析一体,用户给与的时间文本可以有很多非时间内容,使得解析更加便捷。
- 对语义话时间进行计算,对其他语言进行支持。
- 使用正则规则,后续增加特殊时间格式较为方便。
项目特色
- 模糊抽取解析
- 解析速度快
- 支持自定义解析规则
使用方法
安装
pip install gggdtparser
使用
import gggdtparser
parse_dt = gggdtparser.parse("发布:2022/02/02 02:02:02,来源:xxx网")
print(parse_dt) # 2022-02-02 02:02:02
测试
import datetime
import gggdtparser
parse_dt = gggdtparser.parse("发布:2022/02/02 02:02:02,来源:xxx网")
is_right = gggdtparser.check(parse_dt, datetime.datetime(year=2022, month=2, day=2, hour=2, minute=2, second=2))
print(is_right) # True
使用案例
详细案例
- 参考dtformat.md中支持的格式。
- 参考test.py中的测试案例。
特色案例
- 语义时间的支持
il y a 26 minutes
il y a 1 heure
Publié aujourd’hui à 10h34, modifié à 10h39
23分鐘前
......
- 允许噪音
发布于:2023/2/20
Hoje Macau - 4 Abr 2023
2023年04月10日 07:46 来源:新闻网
......
- 支持多种语言
31 март 2023 # 俄语
31 de marzo de 2023 # 西班牙语
27 Fev 2023 # 卡拜尔语
......
- 指定formats解析
import gggdtparser
dt = gggdtparser.parse("2023-02-20", format_list=["%Y-%m-%d"])
print(dt)
- 指定正则解析
正则使用有名分组形式,对应关系如下:
| 关键词 | 含义 | 示例 |
|---|---|---|
| Y | 年 | (?P<Y>\d{4})年 |
| m | 月 | (?P<m >\d{1,2})月 |
| d | 日 | (?P<d>\d{1,2})日 |
| H | 时 | (?P<H>\d{1,2})时 |
| M | 分 | (?P<M>\d{1,2})分 |
| S | 秒 | (?P<S>\d{1,2})秒 |
| bY | 在...年前 | (?P<bY>\d+)\s*(年)\s*(前) |
| bm | 在...月前 | (?P<bm>\d+)\s*(月)\s*(前) |
| bd | 在...日前 | (?P<bd>\d+)\s*(天)\s*(前) |
| bH | 在...时前 | (?P<bH>\d+)\s*(小时)\s*(前) |
| bM | 在...分前 | (?P<bM>\d+)\s*(分钟)\s*(前) |
| bS | 在...秒前 | (?P<bS>\d+)\s*(秒)\s*(前) |
| ba | 在...星期前 | (?P<ba>\d+)\s*(周)\s*(前) |
| wY | 在...年内 | (?P<wY>\d+)\s*(年)\s*(内) |
| wm | 在...月内 | (?P<wm>\d+)\s*(月)\s*(内) |
| wd | 在...日内 | (?P<wd>\d+)\s*(天)\s*(内) |
| wH | 在...时内 | (?P<wH>\d+)\s*(小时)\s*(内) |
| wM | 在...分内 | (?P<wM>\d+)\s*(分钟)\s*(内) |
| wS | 在...秒内 | (?P<wS>\d+)\s*(秒)\s*(内) |
| wa | 在...星期内 | (?P<wa>\d+)\s*(周)\s*(内) |
| aY | 在...年后 | (?P<wY>\d+)\s*(年)\s*(后) |
| am | 在...月后 | (?P<wm>\d+)\s*(月)\s*(后) |
| ad | 在...日后 | (?P<wd>\d+)\s*(天)\s*(后) |
| aH | 在...时后 | (?P<wH>\d+)\s*(小时)\s*(后) |
| aM | 在...分后 | (?P<wM>\d+)\s*(分钟)\s*(后) |
| aS | 在...秒后 | (?P<wS>\d+)\s*(秒)\s*(后) |
| aa | 在...星期后 | (?P<wa>\d+)\s*(周)\s*(后) |
| sd | 今天/昨天/前天/刚刚 | (?P<sd>前天) |
| apm | 上午下午 | (?P<apm>am) |
示例如下:
import gggdtparser
dt = gggdtparser.parse("2023-02-20",
regex_list=[r"(?P<Y>\d{4})(?P<m>\d{2})(?P<d>\d{2})"])
print(dt)
- 自定义翻译
import gggdtparser
def translate(s):
# 翻译
return ""
dt = gggdtparser.parse("xxx", translate_func=translate)
print(dt)
- 时间范围解析
import datetime
from gggdtparser import parse_frame
print(parse_frame('2022年10月1日至2023年10月1日'))
print(parse_frame('10个月', regex_list=[None, ('(?P<am>\d+)\s*(个)?月',)], base_datetime=datetime.datetime(year=2023, month=1, day=1)))
待完善
- 兼容更多语言
- 对时区的解析
关于作者
- 邮箱:1194542196@qq.com
- 微信:hu1194542196
- 目前对常见的时间格式解析支持比较全,但是一些特殊的时间格式和其他语言的支持不够完善,如果遇到解析bug 或不能解析的时间格式,可以私信作者,你们的提供越多,本库才能更完善。
Metadata
Release files for gggdtparser 0.1.6
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| gggdtparser-0.1.6.tar.gz | 20.7 kB | Without provenance | Details for gggdtparser-0.1.6.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| gggdtparser-0.1.6-py3-none-any.whl | Python 3 | none | any | Details for gggdtparser-0.1.6-py3-none-any.whl |
Total release size: 51.1 kB
Release files / gggdtparser-0.1.6.tar.gz
| Download URL | gggdtparser-0.1.6.tar.gz |
|---|---|
| Size | 20.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
30f057b651dcf636bf2f8a573160b067790091a60b68b185f10017acc94147ad
|
|
BLAKE2b-256 checksum How to use checksums |
2499d65497890e97bd6d97f5d5b06d33c3247da8c414011f10edc7f649712135
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.9.5
|
Release files / gggdtparser-0.1.6-py3-none-any.whl
| Download URL | gggdtparser-0.1.6-py3-none-any.whl |
|---|---|
| Size | 30.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
fea1430eb8861914853106b481d6da4bcf9c0eb735bcf92504241ebe2404d5a4
|
|
BLAKE2b-256 checksum How to use checksums |
b6654d7f25efea5be1d043dc6f00679dcba66183e0c2e09d622d3ab3f71bafcd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.9.5
|
Security for gggdtparser 0.1.6
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.