def urlsafestring(string): from string import strip, rstrip, lstrip, replace string = str(string) badchars = [".",",","+","=","%","#","!","@","$","^","*","(",")","\\","/","\"","[","]","{","}","|","'","?"] for b in badchars: string = replace(string, b, "") string = rstrip(string) string = lstrip(string) string = replace(string, "-", "_") string = replace(string, " ", "_") string = replace(string, "__", "_") return string