Skip to content

Commit

Permalink
fix bugs in get_string_num
Browse files Browse the repository at this point in the history
  • Loading branch information
xianhu committed Feb 1, 2018
1 parent 83b931b commit 0e5071b
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions spider/utilities/util_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
]


def get_string_num(string):
def get_string_num(string, ignore_sign=False):
"""
get a float number from a string
"""
string_re = re.search(r"(?P<num>\d+(\.\d+)?)", get_string_strip(string.replace(",", ""), replace_char=""), flags=re.IGNORECASE)
return float(string_re.group("num")) if string_re else 0.0
string_re = re.search(r"(?P<sign>-*?)(?P<num>\d+(\.\d+)?)", get_string_strip(string.replace(",", ""), replace_char=""), flags=re.IGNORECASE)
return float((string_re.group("sign") if not ignore_sign else "") + string_re.group("num")) if string_re else 0.0


def get_string_strip(string, replace_char=" "):
Expand Down

0 comments on commit 0e5071b

Please sign in to comment.