我有以下代码:
text = 'hello world 2,000 3,000'
text = text.replace(' ', '|')
print text
输出是:
hello|world|2,000|3,000
我想用'|'分隔但我希望输出分为三列。我不希望单词彼此分隔,只是数字中的单词:
hello world|2,000|3,000
我该怎么做?
请您参考如下方法:
使用正则表达式替换:
import re
text = 'hello world 2,000 3,000'
print re.sub(r'\s(\d)', '|\\1', text)
这只会为前面有空格和数字的内容插入管道标记。
