我有一个包含单词和数字的文本。我将举一个具有代表性的文本示例:
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"
我快到了,但无法弄清楚最后的和平。