def stripcom(s,delim="#"): c=0 state=None lits=["'",'"'] while c0 and s[c-1]!="\\"): if state==None: state=lits.index(s[c]) else: if s[c]==lits[state]: state=None if state==None and s[c:c+len(delim)]==delim: return s[:c] c=c+1 return s