当前位置:网站首页>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
” 了 .
边栏推荐
- No one consults when doing research and does not communicate with students. UNC assistant professor has a two-year history of teaching struggle
- MySQL表字段调整
- Explanation report of the explosion
- 机械臂速成小指南(九):正运动学分析
- How can the boss choose programmers to help me with development?
- Transfer the idea of "Zhongtai" to the code
- How to introduce devsecops into enterprises?
- Bugku's Eval
- Detailed explanation of QT creator breakpoint debugger
- sql server char nchar varchar和nvarchar的区别
猜你喜欢
Data communication foundation OSPF Foundation
Transfer the idea of "Zhongtai" to the code
Analytic hierarchy process of mathematical modeling (including Matlab code)
Creation and use of thymeleaf template
19.[STM32]HC_SR04超声波测距_定时器方式(OLED显示)
Optional parameters in the for loop
I spring and autumn blasting-2
lv_font_conv离线转换
F. Min cost string problem solving Report
Creation and optimization of MySQL index
随机推荐
No one consults when doing research and does not communicate with students. UNC assistant professor has a two-year history of teaching struggle
MySQL 巨坑:update 更新慎用影响行数做判断!!!
Nine hours, nine people, nine doors problem solving Report
Advanced level of static and extern
OSI seven layer model
I spring and autumn blasting-2
Reasons and solutions for redis cache penetration and cache avalanche
Summary of the second lesson
通过的英特尔Evo 3.0整机认证到底有多难?忆联科技告诉你
Bugku's eyes are not real
机械臂速成小指南(九):正运动学分析
ionic cordova项目修改插件
Definition of episodic and batch
Can I pass the PMP Exam in 20 days?
Magic methods and usage in PHP (PHP interview theory questions)
把 ”中台“ 的思想迁移到代码中去
sql server char nchar varchar和nvarchar的区别
记录一下树莓派搭建环境中遇到的坑。。。
Hongmeng system -- Analysis from the perspective of business
D-snow halo solution