String to number (int/float) in nested lists

When String.ToNumber fails you, this python script will come to help. Works on nested lists.

# by Alexander Berg | 2019-12-09 | Automated Design Solutions

def c2p(s):
	if "," in s:
		return s.replace(",", ".")
	else:
		return s

def num(s):
    try:
    	return float(s)
    except:
    	return s

def toInt(s):
	try:
		if s % 1 == 0:
			return int(s)
		else:
			return s
	except:
		return s
		
def co(s):
	return toInt(num(c2p(s)))
	
def cn(s):
	if (isinstance(s, list)):
		return [cn(i) for i in s]
	else:
		return co(s)

OUT = [cn(i) for i in IN[0]]
2 Likes