Python Forum
using .replace() - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: using .replace() (/thread-20732.html)



using .replace() - Pedroski55 - Aug-28-2019

Hi,

I have a small text, just 12 lines. Each line has a word or phrase I want to remove and replace with a dropdownbox containing 10 words. I read the file in with .readlines() then check each line in a loop.

One word is ate.

The first line contains the word late. My program also removes the ate from late. That is not what I want.

Is there a simple way to prevent this? How to remove only an exact match?

I could .split() the line, but that would make it harder to remove phrases. Works great for single words!


RE: using .replace() - Yoriz - Aug-28-2019

Matching whole words in find/replace script


RE: using .replace() - Pedroski55 - Aug-28-2019

I want to insert html dropdownboxes in a text. I can do this successfully when I split each line of text and just replace 1 word.
Now I would like to replace phrases as well.

I have:
words = ['ate', 'had come in', 'lived', 'put', 'used to collect', 'were sleeping', 'would clamber', 'would go', 'would walk']

lines[1] = We lived there in the late 1990s.

I thought I could try re

for line in lines:
        for i in range(0, len(words)):
            if words[i] in line:
                print('Found ' + words[i])
                newselectionString = selectionString.replace('GX', 'G' + str(startInt))
                newline = re.sub(words[i], newselectionString, line)
                newLines.append(newline)
                startInt +=1
But this also removes ate from late in the second line.



How do I tell re "Only replace ate"?? Can I set a flag?

lines[1] = We lived there in the late 1990s.


RE: using .replace() - perfringo - Aug-28-2019

If you don't care of punctuation then you can rely of difference between 'ate' and 'late' which is that former starts with whitespace (and ends with whitespace as well if there is no punctuation)

>>> s = 'We lived there in the late 1990s. We ate very healthy.'                                                         
>>> s.replace(' ate ', ' slept ')                                                                                        
'We lived there in the late 1990s. We slept very healthy.'



RE: using .replace() - Pedroski55 - Aug-28-2019

Got some help from stackoverflow, works great:

for line in lines:
        for i in range(0, len(words)):
            if words[i] in line:
                print('Found ' + words[i])
                newselectionString = selectionString.replace('GX', 'G' + str(startInt))
                newline = re.sub(r"\b{}\b".format(words[i]), newselectionString, line)
                newLines.append(newline)
                startInt +=1



RE: using .replace() - perfringo - Aug-28-2019

I have to disappoint you. Despite 'works great' (does it?) this is the code you should not use without refactoring.

You should never (and I mean NEVER) use this:

for i in range(0, len(words)):
            if words[i] in line:
You should always (and I mean ALWAYS) use this:

for word in words:
   if word in line



RE: using .replace() - Pedroski55 - Aug-29-2019

You are right!
The re.sub() part works great.

The rest caused me problems, so I went back to .replace(). I got round the problem by reading the words I want to remove and replace with dropdownbox like this: I first read the words into a dictionary, 1 line of text at a time, then remove and replace them with dropdownbox html.

for key in literalsToTakeout.keys():
        string = lines_gapped[key]
        newselectionString = selectionString.replace('X', str(startInt))
        newline = string.replace(literalsToTakeout[key], newselectionString)
        lines_gapped[key] = newline # reassign lines_gapped[key]
        startInt +=1
        
    lines_gapped.append('\n </p> \n\n')   
    newText = ' '.join(lines_gapped)
    print('Are we appending or writing? Enter a or w. Enter w if this is the first thing, otherwise enter a.')
    how = input()
    dropdownText = open(pathToParagraph + 'paragraph', how)
    dropdownText.write(newText)
    #dropdownText.replace('.', '.\n')
    dropdownText.close()
This gives me a satisfactory result, although I'm sure it is very ugly in your eyes! I generated a webpage with the html I made with this. The webpage diplays OK.
I will try out your suggestion, thanks!