Skip to content

Instantly share code, notes, and snippets.

@andjc
Last active April 7, 2022 23:03
Show Gist options
  • Save andjc/adbb9f0581c11899f36a9ed2dda0b002 to your computer and use it in GitHub Desktop.
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.
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