In my program, the user enters a term that I process before sending. Part of this process is to change all instances of "and", "or" and "no" to uppercase letters, but leaving them intact.
I can't use string.upper() because it changes everything to uppercase; or string.replace() , because if "and" is in another word in a string, for example. the salamander will also change this to salamANDer. I believe my best option is the re.sub() function. This allows me to change complete words that are perfect. The next problem: I need to execute the re.sub() function for every change I want to make. Can I make one expression to make all the changes? What I did is not mistaken, but I do not think its necessarily good practice:
>>import urllib2 >>import re >>query = 'Lizards and Amphibians not salamander or newt' >>query=re.sub(r'\bnot\b', 'NOT',query) >>query=re.sub(r'\bor\b', 'OR',query) >>query=re.sub(r'\band\b', 'AND',query) >>query = urllib2.quote("'"+query+"'") >>print query %27Lizards%20AND%20Amphibians%20NOT%20salamander%20OR%20newt%27
source share