I want to replace space with comma for this particular pattern only not all spaces: "21311 asd"
Input:
["dog,cat ax 21311 asd","131 MAN"]
Desired output:
["dog,cat ax 21311,asd","131,MAN"]
Code:
input = ["dog,cat ax 21311 asd","131 MAN"]
new_list = []
for i in input:
word = re.findall(r"\d*\s[a-zA-Z]*" , i)
new_word = re.sub(word , r"\d*\s[a-zA-Z]*" , i)
new_list = new_list + new_word
print(new_list)
I know this is wrong syntax, but I don't know how to do it using Regex or any other method.
I'm using Python 3 and Jupyter notebook.
str.replacecan do. Also, you should avoid shadowing built-in names likeinput. The reference to theinputfunction will be overwritten by your list.