From f24fe9d7e057c58e3f578a6d22ea56f98c8f5fbc Mon Sep 17 00:00:00 2001 From: Larry Bowden Date: Sat, 7 Sep 2024 00:04:45 -0500 Subject: [PATCH] added cleanup for number_convert to address errors --- finvizfinance/util.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/finvizfinance/util.py b/finvizfinance/util.py index 126f775..13d5a5e 100644 --- a/finvizfinance/util.py +++ b/finvizfinance/util.py @@ -136,16 +136,17 @@ def image_scrap_function(url, chart, timeframe, urlonly): def number_covert(num): - """covert number(str) to number(float) + """Convert number(str) to number(float) Args: - num(str): number of string + num(str): number as a string Return: - num(float): number of string + num(float or None): number converted to float or None """ - if num == "-": + if not num or num == "-": # Check if the string is empty or is "-" return None - elif num[-1] == "%": + num = num.strip() # Remove any surrounding whitespace + if num[-1] == "%": return float(num[:-1]) / 100 elif num[-1] == "B": return float(num[:-1]) * 1000000000 @@ -154,7 +155,7 @@ def number_covert(num): elif num[-1] == "K": return float(num[:-1]) * 1000 else: - return float("".join(num.split(","))) + return float(num.replace(",", "")) # Remove commas and convert to float def format_datetime(date_str):