Abend,
ich habe schon etliche seiten durchforstet aber immernoch nicht das richtige gefunden(oder falsch angewendet )
ich möchte einfach alle sonderzeichen die in einem string vorkommen könnten löschen, zb:
a = "Ha---- § l l +++****+++'''### o=="
...lösche sonderzeichen
---> "Hallo"
habs mit replace, re.sub etc verucht aber klappt nicht wirklich
gibt es da ein andere Methode?
Gruß
Sonderzeichen eines strings eliminieren
Spock hat geschrieben:Die einfachste Logik ist die beste.
Code: Alles auswählen
#!/usr/bin/env python
#-*- coding: iso-8859-1 -*-
a = "Ha---- § l l +++****+++'''### o=="
sond = """- §+*'#="""
for i in sond:
a = a.replace(i, "")
print a
Gruß
Ich hab auch was
Code: Alles auswählen
>>> f = lambda x: ''.join(i for i in x if i.isalnum())
>>> f('Ha---- § l l +++****+++""### o==')
'Hallo'
the more they change the more they stay the same
Danke
Das "Problem" mit den Leerzeichen ist mir bekannt
Das "Problem" mit den Leerzeichen ist mir bekannt
Code: Alles auswählen
>>> all_allowed = string.ascii_letters + string.digits + ' '
>>> all_allowed
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 '
>>> f = lambda x: ''.join(c for c in x if c in all_allowed)
>>> f('Ha---- § l l +++****+++""### o==')
'Ha l l o'
>>>
the more they change the more they stay the same
Holgi22 hat geschrieben:habs mit replace, re.sub etc verucht aber klappt nicht wirklich
Code: Alles auswählen
>>> a = "Ha---- § l l +++****+++'''### o=="
>>> import re
>>> a = re.sub(r'\W+', '', a)
Achtung: User ist ein Python-Lehrling!
Es werden auch keine Strings verändert, sondern neue erzeugt.TRiBLy hat geschrieben:Irgendwie steh ich auf'm Schlauch. Ich dachte Strings seien unveränderbar, aufgrund ihrer Funktion als Key
Das Leben ist wie ein Tennisball.