[Python-es] invalid literal for int() with base 10
monoBOT
monobot.soft en gmail.com
Dom Dic 18 12:28:15 CET 2011
o try en int() y capturando el error float()
El 17 de diciembre de 2011 19:40, Jose Luis Bazán <cheru13 en hotmail.com>escribió:
> El problema es que la lista de strings contiene strings equivalentes a
> numeros reales.
> Debes usar:
> Listareal = [float(x) for x in listastrings]
> o
> Listaenteros = [int(float(x)) for x in listastrings]
>
> espero que te sirva de ayuda
> Saludoa
>
> -----Original Message-----
>
> From: Diego Uribe Gamez
> Sent: 17 Dec 2011 17:09:26 GMT
> To: pyar,La lista de python en castellano
> Subject: [Python-es] invalid literal for int() with base 10
>
> Resulta que el problema que tengo es un poco complejo y es que la Lista
> que tengo contiene Strings pero en realidad son números.
>
> Intente la función normal de python para convertir todos los Strings en
> Enteros:
>
> for i in range(len(self.Cadenastring)):
> self.Cadenapura = int(self.Cadenastring[i])
>
> Cadenastring = ['2', '012642000444971' , '151938.00' , '0440.132117',
> '0.0', '0.0', '171211', '5.5', ]
>
> *Error:*
> self.Cadenapura = int(self.Cadenastring[i])
> ValueError: invalid literal for int() with base 10: ''
>
> *Encontré esta información:*
>
> http://docs.python.org/library/string.html
>
> Convert string *s* to an integer in the given *base*. The string must
> consist of one or more digits, optionally preceded by a sign (+ or -). The
> *base* defaults to 10. If it is 0, a default base is chosen depending on
> the leading characters of the string (after stripping the sign): 0x or 0X means
> 16, 0 means 8, anything else means 10. If *base* is 16, a leading 0x or 0X
> is always accepted, though not required. This behaves identically to the
> built-in function int()<http://docs.python.org/library/functions.html#int>
> when passed a string. (Also note: for a more flexible interpretation of
> numeric literals, use the built-in function eval()<http://docs.python.org/library/functions.html#eval>
> .)
> *El problema:*
> No se trata de una Lista de números matemáticos, es solo un texto en
> con números escritos que debo cambiarles el Type pero no logro hacerlo?
>
> --
> *Diego Alonso Uribe Gamez*
> ------------------------------
>
> *Desarrollador web*
>
> Twitter: @DiegoUG <http://www.twitter.com/DiegoUG>
>
> Google+: http://gplus.to/diegoug
> ------------------------------
>
>
>
> _______________________________________________
> Python-es mailing list
> Python-es en python.org
> http://mail.python.org/mailman/listinfo/python-es
> FAQ: http://python-es-faq.wikidot.com/
>
>
--
*monoBOT*
Visite mi sitio(Visit my site): monobotblog.alvarezalonso.es
------------ próxima parte ------------
Se ha borrado un adjunto en formato HTML...
URL: <http://mail.python.org/pipermail/python-es/attachments/20111218/794be6af/attachment.html>
Más información sobre la lista de distribución Python-es