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 に答える