简体   繁体   中英

How to remove punctuation in Python without leaving a space

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?

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.

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())

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM