我有一段包含单词和数字的文本。以下是一个代表性的例子:
我想把它转换成类似于:
所以,去除标点符号(可以是
我使用了以下代码:
结果是:
我快到了,但是想不出最后一部分的意思。
string = "This is a 1example of the text. But, it only is 2.5 percent of all data"
我想把它转换成类似于:
"This is a 1 example of the text But it only is 2.5 percent of all data"
所以,去除标点符号(可以是
.
,
或任何 string.punctuation
中的其他符号),并在将数字和单词连接在一起时加上空格。但保留像我的示例中的2.5这样的浮点数。我使用了以下代码:
item = "This is a 1example of the text. But, it only is 2.5 percent of all data"
item = ' '.join(re.sub( r"([A-Z])", r" \1", item).split())
# This a start but not there yet !
#item = ' '.join([x.strip(string.punctuation) for x in item.split() if x not in string.digits])
item = ' '.join(re.split(r'(\d+)', item) )
print item
结果是:
>> "This is a 1 example of the text. But, it only is 2 . 5 percent of all data"
我快到了,但是想不出最后一部分的意思。
2.5
等类似内容。 - deltascience