I have a list of strings in the following form:
d = ['0.04M sodium propionate', ' 0.02M sodium cacodylate', ' 0.04M bis-tris propane', ' pH 8.0 ']
I want to remove x.xxM but keep the number following pH. I tried the following:
import re
for i in range(len(d)):
d[i] = d[i].translate(None,'[1-9]+\.*[0-9]*M')
which produced the following:
>>> d
['4 sodium propionate', ' 2 sodium cacodylate', ' 4 bistris propane', ' pH 8 ']
removing the .0 from the pH as well. I think translate() does not take order into account, right? Also, I don't understand why the 4, 2 etc. still remain in either of the elements. How could I remove the pieces of strings strictly in the form [1-9]+\.*[0-9]*M (meaning that there should be a digit, maybe followed by a . and zero or more digits, and an M)?
Edit: I know realize that using regex doesn't work with translate(). It matches the 0, ., and M and removes them. I guess I can try re.search(), find the exact piece of string, and then do sub().
import re)?translate? because it's totally unfit for the jobhelp(re)