Last active
April 7, 2022 23:03
-
-
Save andjc/adbb9f0581c11899f36a9ed2dda0b002 to your computer and use it in GitHub Desktop.
Convert digits (as string) to int or float as appropriate. Currenttly des not support ideographic numbers or algorithmic numbers.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unicodedataplus as ud | |
import regex as re | |
def convert_digits(s, sep = (",", ".")): | |
nd = re.compile(r'^-?\p{Nd}[,.\u066B\u066C\u0020\u2009\p{Nd}]*$') | |
tsep, dsep = sep | |
if nd.match(s): | |
s = s.replace(tsep, "") | |
s = ''.join([str(ud.decimal(c, c)) for c in s]) | |
if dsep in s: | |
return float(s.replace(dsep, ".")) if dsep != "." else float(s) | |
return int(s) | |
return s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment