From b5d8968144e76090870f8563dc1bce910fba668c Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Sat, 17 Apr 2021 17:22:59 -0700 Subject: [PATCH] add convert_to_int function --- jc/utils.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/jc/utils.py b/jc/utils.py index e6d88f74..186592f7 100644 --- a/jc/utils.py +++ b/jc/utils.py @@ -83,6 +83,25 @@ def has_data(data): return True if data and not data.isspace() else False +def convert_to_int(value): + """ + Converts string input to integer by stripping all non-numeric characters + + Parameters: + + value: (string) Input value + + Returns: + integer/None Integer if successful conversion, otherwise None + """ + try: + value = int(re.sub('[^0-9]', '', value)) + except (ValueError, TypeError): + return None + + return value + + class timestamp: """ Input a date-time text string of several formats and convert to a naive or timezone-aware epoch timestamp in UTC