From cc7fee7235a6764338bbb0a908667c129d765b6a Mon Sep 17 00:00:00 2001 From: joz <952597205@qq.com> Date: Fri, 18 Jun 2021 15:16:40 +0800 Subject: [PATCH 1/2] =?UTF-8?q?1=EF=BC=8C=E9=AA=8C=E8=AF=81=E6=98=AF?= =?UTF-8?q?=E5=90=A6=E9=9C=80=E8=A6=81=E5=A4=A7=E5=B0=8F=E5=86=99=E8=BD=AC?= =?UTF-8?q?=E5=8C=96=202=EF=BC=8C=E5=AF=B9=E5=B7=B2=E7=9F=A5=E5=A4=84?= =?UTF-8?q?=E7=90=86=E4=B8=8D=E5=BD=93=E6=96=B9=E6=B3=95=E4=BF=AE=E6=AD=A3?= =?UTF-8?q?=203=EF=BC=8C=E5=85=BC=E5=AE=B9=E7=B1=BB'15=20=E5=B0=8F?= =?UTF-8?q?=E6=97=B6=E5=89=8D'=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- feapder/utils/tools.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/feapder/utils/tools.py b/feapder/utils/tools.py index 8b8a4e5b..ce7f97c4 100644 --- a/feapder/utils/tools.py +++ b/feapder/utils/tools.py @@ -1513,6 +1513,13 @@ def transform_lower_num(data_str: str): '九': '9', '十': '0', } + pattern = f'[{"|".join(num_map.keys())}|零]' + res = re.search(pattern, data_str) + if not res: + # 如果字符串中没有包含中文数字 不做处理 直接返回 + return data_str + + data_str = data_str.replace('0', '零') for n in num_map: data_str = data_str.replace(n, num_map[n]) @@ -1537,7 +1544,7 @@ def transform_lower_num(data_str: str): @run_safe_model("format_time") def format_time(release_time, date_format="%Y-%m-%d %H:%M:%S"): release_time = transform_lower_num(release_time) - release_time = release_time.replace('日','天') + release_time = release_time.replace('日', '天').replace(' ', '') if "年前" in release_time: years = re.compile("(\d+)年前").findall(release_time) From 98105fde86f75d4acfddc7e1eb589a75221b3860 Mon Sep 17 00:00:00 2001 From: Boris <564773807@qq.com> Date: Fri, 18 Jun 2021 17:50:51 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=AE=8C=E5=96=84forat=5Ftime?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- feapder/utils/tools.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/feapder/utils/tools.py b/feapder/utils/tools.py index 238a408f..17feb443 100644 --- a/feapder/utils/tools.py +++ b/feapder/utils/tools.py @@ -1540,7 +1540,7 @@ def transform_lower_num(data_str: str): # 如果字符串中没有包含中文数字 不做处理 直接返回 return data_str - data_str = data_str.replace('0', '零') + data_str = data_str.replace("0", "零") for n in num_map: data_str = data_str.replace(n, num_map[n]) @@ -1565,39 +1565,39 @@ def transform_lower_num(data_str: str): @run_safe_model("format_time") def format_time(release_time, date_format="%Y-%m-%d %H:%M:%S"): release_time = transform_lower_num(release_time) - release_time = release_time.replace('日', '天').replace(' ', '') + release_time = release_time.replace("日", "天") if "年前" in release_time: - years = re.compile("(\d+)年前").findall(release_time) + years = re.compile("(\d+)\s*年前").findall(release_time) years_ago = datetime.datetime.now() - datetime.timedelta( days=int(years[0]) * 365 ) release_time = years_ago.strftime("%Y-%m-%d %H:%M:%S") elif "月前" in release_time: - months = re.compile("(\d+)月前").findall(release_time) + months = re.compile("(\d+)\s*月前").findall(release_time) months_ago = datetime.datetime.now() - datetime.timedelta( days=int(months[0]) * 30 ) release_time = months_ago.strftime("%Y-%m-%d %H:%M:%S") elif "周前" in release_time: - weeks = re.compile("(\d+)周前").findall(release_time) + weeks = re.compile("(\d+)\s*周前").findall(release_time) weeks_ago = datetime.datetime.now() - datetime.timedelta(days=int(weeks[0]) * 7) release_time = weeks_ago.strftime("%Y-%m-%d %H:%M:%S") elif "天前" in release_time: - ndays = re.compile("(\d+)天前").findall(release_time) + ndays = re.compile("(\d+)\s*天前").findall(release_time) days_ago = datetime.datetime.now() - datetime.timedelta(days=int(ndays[0])) release_time = days_ago.strftime("%Y-%m-%d %H:%M:%S") elif "小时前" in release_time: - nhours = re.compile("(\d+)小时前").findall(release_time) + nhours = re.compile("(\d+)\s*小时前").findall(release_time) hours_ago = datetime.datetime.now() - datetime.timedelta(hours=int(nhours[0])) release_time = hours_ago.strftime("%Y-%m-%d %H:%M:%S") elif "分钟前" in release_time: - nminutes = re.compile("(\d+)分钟前").findall(release_time) + nminutes = re.compile("(\d+)\s*分钟前").findall(release_time) minutes_ago = datetime.datetime.now() - datetime.timedelta( minutes=int(nminutes[0]) ) @@ -1629,7 +1629,7 @@ def format_time(release_time, date_format="%Y-%m-%d %H:%M:%S"): else: release_time = str(int(get_current_date("%Y")) - 1) + "-" + release_time - template = re.compile("(\d{4}-\d{2}-\d{2})(\d.*)") + template = re.compile("(\d{4}-\d{1,2}-\d{1,2})(\d.*)") release_time = re.sub(template, r"\1 \2", release_time) release_time = format_date(release_time, new_format=date_format)