当前位置:网站首页>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 - routing communication between VLANs
- MySQL5.7的JSON基本操作
- CSRF, XSS science popularization and defense
- sql server char nchar varchar和nvarchar的区别
- Interval DP (gravel consolidation)
- I spring web upload
- Detailed explanation of QT creator breakpoint debugger
- wyt 。。
- The difference between abstract classes and interfaces in PHP (PHP interview theory question)
- Data communication foundation ACL access control list
猜你喜欢
keep-alive
Lesson 4 knowledge summary
Thymeleaf uses background custom tool classes to process text
Bugku's steganography
Anti shake and throttling
Ionic Cordova project modification plug-in
Data communication foundation OSPF Foundation
Information collection of penetration test
18.[STM32]读取DS18B20温度传感器的ROM并实现多点测量温度
Talk about your understanding of microservices (PHP interview theory question)
随机推荐
SQL injection sqllabs (basic challenges) 1-10
Summary of the second lesson
MySQL giant pit: update updates should be judged with caution by affecting the number of rows!!!
Appium自动化测试基础 — APPium基础操作API(一)
wyt 。。
Talk about your understanding of microservices (PHP interview theory question)
ionic cordova项目修改插件
I include of spring and Autumn
Garbage collection mechanism of PHP (theoretical questions of PHP interview)
示例项目:简单的六足步行者
Redis' transaction mechanism
Bubble sort, insert sort
Redis distributed lock principle and its implementation with PHP (1)
Data communication foundation OSPF Foundation
定义严苛标准,英特尔Evo 3.0正在加速PC产业升级
Codasip为RISC-V处理器系列增加Veridify安全启动功能
OSI 七层模型
Nine hours, nine people, nine doors problem solving Report
Object. defineProperty() - VS - new Proxy()
Where is the operation of convertible bond renewal? Is it safer and more reliable to open an account