0

How to replace the space that follows a digit into comma using python and re?

For example,

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)

but that gives me 0, 1, *, *, *,

Shuda Li
  • 199
  • 1
  • 11

1 Answers1

0

Another way, without re:

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
Lucas
  • 6,869
  • 5
  • 29
  • 44
  • Thanks Ibellomo, but the spaces between these digits are arbitrary. Zero's solution turns out to be very useful. re.sub(r"(\d+)\s", r"\1,", tex). – Shuda Li Dec 28 '16 at 17:48
  • I do not know `re` and that's why I avoid them. – Lucas Dec 28 '16 at 20:53