user3429227
user3429227

Reputation:

delete characters that are not letters, numbers, whitespace?

community,

I need to clean a string, so that it will contain only letters, numbers and whitespace. The string momentarily consists of different sentences.

I tried:

for entry in s:
if not isalpha() or isdigit() or isspace:
    del (entry)
else: s.append(entry) # the wanted characters should be saved in the string, the rest should be deleted

I am using python 3.4.0

Upvotes: 1

Views: 328

Answers (2)

Tom Fenech
Tom Fenech

Reputation: 74685

You can use this:

clean_string = ''.join(c for c in s if c.isalnum() or c.isspace())

It iterates through each character, leaving you only with the ones that satisfy at least one of the two criteria, then joins them all back together. I am using isalnum() to check for alphanumeric characters, rather than both isalpha() and isdigit() separately.

You can achieve the same thing using a filter:

clean_string = filter(lambda c: c.isalnum() or c.isspace(), s)

Upvotes: 2

sshashank124
sshashank124

Reputation: 32189

The or does not work the way you think it works in English. Instead, you should do:

new_s = ''

for entry in s:
    if entry.isalpha() or entry.isdigit() or entry.isspace():
        new_s += entry

print(new_s)

Upvotes: 1

Related Questions