Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
using .replace()
#1
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!
Reply
#2
Matching whole words in find/replace script
Reply
#3
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.
Reply
#4
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.'
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
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
Reply
#6
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
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#7
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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Search & Replace - Newlines Added After Replace dj99 3 3,356 Jul-22-2018, 01:42 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020