1

I have been wondering how I can make Python ignore characters inside double quotation marks (") in my find and replace function. My code is:

def findAndReplace(textToSearch, textToReplace,fileToSearch):
    oldFileName  = 'old-' + fileToSearch
    tempFileName = 'temp-' + fileToSearch
    tempFile = open( tempFileName, 'w' )
    for line in fileinput.input( fileToSearch ):
        tempFile.write( line.replace( textToSearch, textToReplace ) )
    tempFile.close()
    # Rename the original file by prefixing it with 'old-'
    os.rename( fileToSearch, oldFileName )
    # Rename the temporary file to what the original was named...
    os.rename( tempFileName, fileToSearch )

Suppose that our file (test.txt) has contents (THIS IS OUR ACTUAL TEXT):

I like your code "I like your code"

and I execute

findAndReplace('code','bucket',test.txt)

which will write the following to my file:

I like your bucket "I like your bucket"

However, I want it to skip the double-quoted part and get this as a result

I like your bucket "I like your code"

What should I add to my source code?

Thanks in advance

Apalala
  • 9,017
  • 3
  • 30
  • 48
bolzano
  • 816
  • 2
  • 13
  • 30
  • 1
    Can there be quotes inside quotes? In other words, does `I like your code "I like "your code""` actually have two quoted strings, `"I like "` and `""`, or one quoted string `"I like "your code""`? – abarnert Jan 02 '14 at 22:22
  • These are the contents of the textfile, if I understand what you've just mentioned – bolzano Jan 02 '14 at 22:23
  • I don't think you understand what I've just mentioned, because you didn't answer my question. But I'll assume that you'd want to treat the example the first way, not the second. – abarnert Jan 02 '14 at 22:33
  • Yes there can be. The reason: The whole string is `'I like your code "I like your code"'` which is ONE string whereas two of its characters are double quotes – bolzano Jan 02 '14 at 22:35
  • I think you're still not getting it, and I don't know how to explain it any better, but let me try one more time: That's one string with one quoted substring inside of it. `I like your code "I like your code" "I really like your code"` is, I assume, one string with _two_ quoted substrings inside of it. As long as the first example I gave it _also_ one string with two quoted substrings inside of it, everything is fine. – abarnert Jan 02 '14 at 22:40

2 Answers2

4
haystack = 'I like your code "I like your code"'
needle = "code"
replacement = "bucket"

parts = haystack.split('"')
for i in range(0,len(parts),2):
   parts[i] = parts[i].replace(needle,replacement)

print '"'.join(parts)

assuming you cannot have nested quotes ...

Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
  • Basically the same as my answer, and you got there first… but I think it's simpler to use slicing and slice assignment to change the even bits, instead of mutating them one by one. – abarnert Jan 02 '14 at 22:39
1

If you don't need to handle quotes inside quotes, or anything like that, this is pretty easy. You could do it with regular expressions. But, since I'm guessing you don't know regexp (or you would have used it in the first place), let's do it with simple strings methods: split your string on quote characters, then replace only the even substrings, then join it back together:

for line in fileinput.input( fileToSearch ):
    bits = line.split('"')
    bits[::2] = [bit.replace(textToSearch, textToReplace) for bit in bits[::2]]
    tempFile.write('"'.join(bits))
abarnert
  • 354,177
  • 51
  • 601
  • 671