简体   繁体   English

如何去除Python中的标点符号而不留空格

[英]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:您可以使用str.split方法拆分字符串,以便将多个空格视为一个,然后通过空格将结果列表连接回一个字符串:

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?如何删除正在生成的空白空间?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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