-3

That is to replace all digits, special characters, non-printing characters. I can't use the re library.

line = line.rstrip() # this get rids of the white spaces
if character in string.digits or character in string.punctuation:
    line = line.replace(character, "")

However, this does not get rid of \; how can I check for this and replace it with nothing? character in "\" does not work.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Gavin
  • 2,784
  • 6
  • 41
  • 78
  • possible duplicate of [Replace all non-alphanumeric characters in a string](http://stackoverflow.com/questions/12985456/replace-all-non-alphanumeric-characters-in-a-string) – fredtantini Oct 31 '14 at 10:12
  • 1
    Why can't you use the re library? It's a standard library. Is this homework? – Joe Oct 31 '14 at 10:12
  • You need to escape the backslash (as it is usually used to escape other characters): `'\\'` – jonrsharpe Oct 31 '14 at 10:13

3 Answers3

3

Just iterate over the string and keep any alphabetic characters. You can do this with a list comprehension and a join.

import string
line = ''.join(c for c in line if c in string.ascii_letters)

Although you can better handle unicode strings with isalpha():

>>> line = u'ABcdef124__++--()zyxôôô999٤end'
>>> print(''.join(c for c in line if c.isalpha()))
ABcdefzyxôôôend

so ô (LATIN SMALL LETTER O WITH CIRCUMFLEX) is retained, but ٤ (ARABIC-INDIC DIGIT FOUR) is not, which seems correct.

mhawke
  • 84,695
  • 9
  • 117
  • 138
1

You should protect "\". Use "\\"

serkos
  • 306
  • 1
  • 4
  • Also, I don't think that it is good idea to delete "\" because it can protect some special symbols – serkos Oct 31 '14 at 10:15
1

Simply use str.isalpha() to check whether a charcter is alphabetic or not:

>>> st='hsfjebhjbe283628362et#@%\\\\\\\\\\\\\\!!@$*)(|<>~\%^%@%^///;...][]=--]9988bjwqgvs'
>>> ''.join(i for i in st if i.isalpha())
'hsfjebhjbeetbjwqgvs'

For alphanumeric use str.isalnum():

>>> ''.join(i for i in st if i.isalnum())
'hsfjebhjbe283628362et9988bjwqgvs'
Irshad Bhat
  • 8,479
  • 1
  • 26
  • 36