如何使用python和re包将数字后面的空格替换为逗号?

时间:2016-12-28 15:31:11

标签: python-3.x

如何使用python和re?

将数字后面的空格替换为逗号

例如,

0,1,0 12 13 - > 0,1,0,12,13;

import re

text = "0, 1, 0 12 13"    
matches = re.sub(r'(\d+)\s','*,', text)
print(matches)

但这给了我 0,1,*,*,*,

1 个答案:

答案 0 :(得分:0)

另一种方式,没有重新:

text = "0, 1, 0 12 13" 
text = text.replace(',', '') #We remove the commas (to leave them all the same)
text.replace(' ', ', ')      # We replace spaces by comma and space