Seite 1 von 1

Typumwandlung

Verfasst: Donnerstag 18. Mai 2006, 11:00
von arslan
Hallo,

ich bin Anfänger, kann jemand mir helfen!
Wieso kann ich nicht ein string -> int umwandeln?

>>> datei=open(r'C:\Eigene Dateien\pythonTest\CMDTest\USD.txt','r')
>>> liste=[line.strip() for line in datei]
>>> datei.close()
>>> print liste[2]
12.779.000
>>> int(liste[0])

Traceback (most recent call last):
File "<pyshell#20>", line 1, in -toplevel-
int(liste[0])
ValueError: invalid literal for int(): 12.914.000
>>>

Danke
arslan

Verfasst: Donnerstag 18. Mai 2006, 11:08
von rumilmirion
Ich denke mal, das Problem dürfte daran liegen, dass dein string zwei Punkte drin hat. Importiere doch das string modul und versuch's so mal:

Code: Alles auswählen

>>>import string
>>>int(liste[0].replace('.',''))

Verfasst: Donnerstag 18. Mai 2006, 11:40
von arslan
genau des wegen hat es nicht geklapt :)
Vielen Dank

Verfasst: Donnerstag 18. Mai 2006, 11:43
von jens

Verfasst: Donnerstag 18. Mai 2006, 13:28
von Joghurt
rumilmirion hat geschrieben:Importiere doch das string modul
Das ist unnötig, Strings stellen die Funktionen direkt bereit.