Replace string except inside quotes?

beliavsky at aol.com beliavsky at aol.com
Fri Dec 3 10:39:39 EST 2004


The code 

for text in open("file.txt","r"):
    print text.replace("foo","bar")[:-1]

replaces 'foo' with 'bar' in a file, but how do I avoid changing text
inside single or double quotes? For making changes to Python code, I
would also like to avoid changing text in comments, either the '#' or
'""" ... """' kind.



More information about the Python-list mailing list