Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何使用python将数字后面的空格替换为逗号并重新打包?_Python 3.x - Fatal编程技术网

Python 3.x 如何使用python将数字后面的空格替换为逗号并重新打包?

Python 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

如何使用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 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
,这就是我避免使用它们的原因。