AutoRpa/rpa_selection/util.py

39 lines
1.0 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 选品用的文本和数字清洗。空值和认不出的数字收成空或 None,不抛。
from __future__ import annotations
from typing import Any
# 收成去掉两端空白的字符串。None 当空。
def normalize_text(value: Any) -> str:
if value is None:
return ""
return str(value).strip()
# 把文本收成整数。空或认不出返回 None。先按浮点再取整,兼容 "10.0"。
def parse_int_or_none(value: Any) -> int | None:
text = normalize_text(value)
if text == "":
return None
try:
return int(float(text))
except ValueError:
return None
# 把文本收成浮点。空或认不出返回 None。逗号当千分位去掉后再试一次。
def parse_float_or_none(value: Any) -> float | None:
text = normalize_text(value)
if text == "":
return None
try:
return float(text)
except ValueError:
cleaned = text.replace(",", "").replace(",", "")
try:
return float(cleaned)
except ValueError:
return None