Una función para extraer los valores númericos de una cadena de texto.
#!/usr/bin/python # -*- coding: utf8 -*- def extractor (cadena): print "Recibido: " + cadena
current = "" sub = "" lista = [] for current in cadena: if (current == "-" and sub == ""): sub += current continue if (current == "+" and sub == "" ): # no hacer nada continue if ((current >= "0" and current <= "9") or current == "."): sub += current else: if (sub != ""): lista.append (sub) sub= "" if (sub != ""): lista.append (sub)
print "Resultado: " + str(lista)
print if __name__=="__main__": extractor ("suma = 34 + 86") extractor ("x = -3 + 5.2") extractor ("y = 7.0--983") extractor ("a = 7 / (9.3+2)")
No hay comentarios:
Publicar un comentario