[Python-es] invalid literal for int() with base 10

Jose Luis Bazán cheru13 en hotmail.com
Sab Dic 17 20:40:02 CET 2011


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
------------------------------
------------ próxima parte ------------
Se ha borrado un adjunto en formato HTML...
URL: <http://mail.python.org/pipermail/python-es/attachments/20111217/349cd715/attachment.html>
------------ próxima parte ------------
_______________________________________________
Python-es mailing list
Python-es en python.org
http://mail.python.org/mailman/listinfo/python-es
FAQ: http://python-es-faq.wikidot.com/


Más información sobre la lista de distribución Python-es