Python 3.x 如何使用python将数字后面的空格替换为逗号并重新打包?
如何使用python和re将数字后面的空格替换为逗号 比如说, 0,1,0123->0,1,0,12,13Python 3.x 如何使用python将数字后面的空格替换为逗号并重新打包?,python-3.x,Python 3.x,如何使用python和re将数字后面的空格替换为逗号 比如说, 0,1,0123->0,1,0,12,13 import re text = "0, 1, 0 12 13" matches = re.sub(r'(\d+)\s','*,', text) print(matches) 但这给了我 0,1,*,*,*,*,另一种方式,无需re: text = "0, 1, 0 12 13" text = text.replace(',', '') #We remove the comm
import re
text = "0, 1, 0 12 13"
matches = re.sub(r'(\d+)\s','*,', text)
print(matches)
但这给了我
0,1,*,*,*,*,另一种方式,无需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
您需要将
*
替换为\1
-见,非常感谢!它就像一个魔术谢谢Ibellomo,但是这些数字之间的空格是任意的。Zero的解决方案非常有用。re.sub(r“(\d+)\s”,r“\1”,tex)。我不知道re
,这就是我避免使用它们的原因。