1

如果输入字符串是这样的:

"that`s not good thing ! you havn`t understand anything ?"

我想将其转换为:

" thats not good thing you havnt understand anything "

这是我想要的吗?

我尝试以下 reg.exp。

line = "that`s not good thing ! you havn`t understand anything ?"
text=re.sub("[^\w]"," ",line).split()

但它不能用于所需的输出。请提出相同的建议。

4

1 回答 1

1

我想你正在寻找这个:

text = re.sub("[^\\w\\s]", "", line)

请注意,除了常规字符之外,您似乎还希望保留空格。

然后,如果您真的在该行中的单词之后,则可以执行text.split()

演示:

In [29]: line = "that`s not good thing ! you havn`t understand anything ?"

In [30]: text=re.sub("[^\\w\\s]","",line)

In [31]: text
Out[31]: 'thats not good thing  you havnt understand anything '

In [32]: text.split()
Out[32]: ['thats', 'not', 'good', 'thing', 'you', 'havnt', 'understand', 'anything']
于 2012-10-09T16:36:34.037 回答