Reputation: 37
I want to remove certain punctuations from a text. I was able to remove my desired characters but it keep leaving a space instead of the character.
In { ) other news tonight,
a Constitutional { | / !! amendment
I have a text such as above and when I process it it becomes
In other news tonight,
a Constitutional !! amendment
Instead of
In other news tonight,
a Constitutional !! amendment
Below is the code I have
for line in lines:
exclude = set('"#$%&\()*+-/:<=>@[\\]^_`{|}')
line = ''.join(ch for ch in line if ch not in exclude)
How do I remove empty spaces that are being produced?
Upvotes: 0
Views: 868
Reputation: 106512
You can split the string with the str.split
method so that multiple spaces are treated as one, and then join the resulting list back into a string by a space:
exclude = set('"#$%&\()*+-/:<=>@[\\]^_`{|}')
for line in lines:
line = ' '.join(''.join(' ' if ch in exclude else ch for ch in line).split())
Upvotes: 0
Reputation: 623
No empty spaces are being created. Your string already has empty spaces between these characters. Removing those characters will not remove the spaces in between them. One potential solution is that I assume you want to remove any areas with more than one consecutive space. Replace your code with:
exclude = set('"#$%&\()*+-/:<=>@[\\]^_`{|}')
for line in lines:
line = ''.join(ch for ch in line if ch not in exclude)
line = ' '.join(line.split())
Which will remove all double spaces.
Upvotes: 1