[Python-de] Zeichen in einem String ersetzen

Andi Albrecht albrecht.andi at googlemail.com
Fr Jul 1 10:38:58 CEST 2011


2011/7/1 Wagner, Frank <F.Wagner at wika.de>
>
> Hallo,
>
>
>
> ich möchte in einem String den Dezimalpunkt durch ein Komma ersetzen, leider hat folgender Code keinen Effekt:
>
>
>
> anzahl = len(werteliste)
>
> for i in range(anzahl-1):
>
>     werteliste[i].replace(".", ",", 1)  # Punkt durch Komma ersetzen zur
>

string.replace() führt keine Inplace-Ersetzung durch, sondern liefert
einen neuen String mit den Ersetzungen zurück.

Du musst also den Rückgabewert von .replace() zurück in werteliste schreiben:

werteliste[i] = werteliste[i].replace(".", ",", 1)


Spricht was dagegen, die Ersetzung mit "werteliste =
werteliste.replace(...)" direkt über den gesamten String zu machen?
Oder muss das zeichenweise geschehen?


Grüße,

-Andi

>
>
> Sehe ich mir das im Debugger an so sehen die einzelnen Listenelemente wie folgt aus:
>
>
>
> werteliste[i]   '+3.00305873E+00'
>
>
>
> Wenn ich darüber steppe bleibt der Punkt allerdings erhalten.
>
> Ich hab keine Ahnung was ich übersehen habe aber vielleicht kann mir einer auf die Sprünge helfen.
>
>
>
> Schon jetzt recht herzlichen Dank
>
>
>
> Mit freundlichen Grüßen / Kind regards
>
>
>
> Frank Wagner
>
> Entwicklung - Prozesstransmitter
>
>
>
>
>
> WIKA
>
> Alexander Wiegand SE & Co. KG
>
> Alexander Wiegand-Straße 30
>
> 63911 Klingenberg am Main
>
>
>
> Tel: 09372-132-8995
>
> Fax: 09372-132-9976
>
> E-mail: f.wagner at wika.de
>
>
>
> --
>
> --
>
>  P   Bevor Sie diese E-Mail ausdrucken, überlegen Sie bitte, ob dies wirklich erforderlich ist. Please think before you print.
>
> --
>
> WIKA Alexander Wiegand SE & Co. KG
>
> Alexander-Wiegand-Straße 30 - 63911 Klingenberg - Germany
>
> Kommanditgesellschaft: Sitz Klingenberg - Amtsgericht Aschaffenburg HRA 1819
>
> Komplementärin: WIKA Verwaltungs SE & Co. KG - Sitz Klingenberg -
>
> Amtsgericht Aschaffenburg HRA 4685
>
> Komplementärin: WIKA International SE - Sitz Klingenberg -
>
> Amtsgericht Aschaffenburg HRB 10505
>
> Vorstand: Alexander Wiegand
>
> Vorsitzender des Aufsichtsrats: Dr. Max Egli
>
> Telefon: (09372) 132-0
>
> Internet: www.wika.com
>
> --
>
> The information contained in this E-Mail and any attached files are strictly confidential and may be subject to legal privilege. If you are not the intended recipient, his representative or the person responsible for delivering the message to the intended recipient, be advised that you have received this message in error and that any dissemination, copying or use of this message or attachment is strictly forbidden, as is the disclosure of the information therein. If you have received this E-Mail in error, please notify us immediately by E-Mail or telephone and delete this message and all its attachments subsequently. All reasonable precautions have been taken to ensure no viruses are present in this E-Mail.
> _______________________________________________
> python-de maillist  -  python-de at python.org
> http://mail.python.org/mailman/listinfo/python-de
>


Mehr Informationen über die Mailingliste python-de