当前位置:网站首页>Modify PyUnit_ Time makes it support the time text of 'xx~xx months'
Modify PyUnit_ Time makes it support the time text of 'xx~xx months'
2022-07-05 15:44:00 【Channing Lewis】
Because you need to use the function of converting time text into standard time , After investigating a variety of third-party packages, I chose pyunit_time, But there are still many unsatisfactory places , So copy out the whole bag , Modify yourself to meet your needs .
Found today pyunit_time I won't support it ‘xx~xx month ’ Statement of the , for example “6~9 month ” Will only recognize 9 month , however “6 month ~9 month ” Can recognize “6 month ” and “9 month ”, So it will “6~9 month ” Turn into “6 month ~9 month ” That's all right. , In fact, it is essentially a problem of omitting time units .
Here is the original code ( The file is /pyunit_time/filters.py):
def get_time_key(string) -> list:
""" Extract the time keyword in the string according to the string The proportion : The night before national day 8 Half past six return : [' National Day ', ' The night before 8 Half past six '] :param string: About the time string of dictation :return: Time keywords """
keys, start, end = [], -1, -1
match = pattern.finditer(string)
for key in match:
start = key.start()
if start == end:
keys[-1] += key.group()
else:
keys.append(key.group())
end = key.end()
return keys
It uses regular matching ,pattern Call the regular expression prepared before , image “x year ”、‘x month ’ etc. , Individual numbers don't match any , So it will be ignored .
After modification :
def get_time_key(string) -> list:
""" Extract the time keyword in the string according to the string The proportion : The night before national day 8 Half past six return : [' National Day ', ' The night before 8 Half past six '] :param string: About the time string of dictation :return: Time keywords """
if '~' in string: # lyc Modify the point , Support “xx~xx month ” Statement of the
begin, *middle, finish = string.split('~')
if begin and finish and begin[-1].isdigit() and not finish[-1].isdigit():
string = string.replace('~', finish[-1] + '~')
keys, start, end = [], -1, -1
match = pattern.finditer(string)
for key in match:
start = key.start()
if start == end:
keys[-1] += key.group()
else:
keys.append(key.group())
end = key.end()
return keys
Add the last time unit to... Before the original processing “~” After the previous separate number , Completed the conversion , Retest and find that you can recognize “6~9 month ” 了 .
边栏推荐
- 示例项目:简单的六足步行者
- Data communication foundation - dynamic routing protocol rip
- How can the boss choose programmers to help me with development?
- 【 note 】 résoudre l'erreur de code IDE golang
- 如何将 DevSecOps 引入企业?
- Bugku telnet
- No one consults when doing research and does not communicate with students. UNC assistant professor has a two-year history of teaching struggle
- Advanced level of static and extern
- Good article inventory
- 2.3 learning content
猜你喜欢

Appium自动化测试基础 — APPium基础操作API(二)

swiper. JS to achieve barrage effect

MySQL giant pit: update updates should be judged with caution by affecting the number of rows!!!

First PR notes
![18.[STM32]读取DS18B20温度传感器的ROM并实现多点测量温度](/img/e7/4f682814ae899917c8ee981c05edb8.jpg)
18.[STM32]读取DS18B20温度传感器的ROM并实现多点测量温度

Your childhood happiness was contracted by it

Fundamentals of data communication - Principles of IP routing

Ctfshow web entry information collection

SQL injection sqllabs (basic challenges) 1-10

六种常用事务解决方案,你方唱罢,我登场(没有最好只有更好)
随机推荐
What are CSRF, XSS, SQL injection, DDoS attack and timing attack respectively and how to prevent them (PHP interview theory question)
Ten billion massage machine blue ocean, difficult to be a giant
记录一下树莓派搭建环境中遇到的坑。。。
JS topic - console log()
First PR notes
Where is the operation of convertible bond renewal? Is it safer and more reliable to open an account
数据库学习——数据库安全性
Common redis data types and application scenarios
CSDN I'm coming
go语言编程规范梳理总结
How to introduce devsecops into enterprises?
具有倍数关系的时钟切换
Detailed explanation of C language branch statements
PHP high concurrency and large traffic solution (PHP interview theory question)
Maximum common subsequence
定义严苛标准,英特尔Evo 3.0正在加速PC产业升级
Vulnhub-Moneybox
episodic和batch的定义
How can the boss choose programmers to help me with development?
Codasip为RISC-V处理器系列增加Veridify安全启动功能