0

This is one of those "I'd know how to do it in C" type questions. :p I'm asking this as similar questions in SO don't have a particular aspect I'm looking for.

I'm essentially looking to find and replace items that also have possessive forms. So if there is a "rabbit" in the list, and also a "rabbit's", then replace "rabbit" with a series of asterisks.

Something along the lines of:

#!/usr/bin/env python

import re

text = open("list.txt", "w")

for line in text:
        test = line
        if re.match(test+"'", line) or re.match(test+"'s", line):
            line = "****" 

However, this clearly won't work as the for each mechanism makes line be used for both iteration and pattern matching.

5
  • 2
    You probably want to open the file in read mode. Commented Dec 15, 2014 at 12:40
  • can your so your input data??? Commented Dec 15, 2014 at 12:40
  • test = line and then re.match(test+"'", line), how is that supposed to work? Commented Dec 15, 2014 at 12:44
  • line = "****" does nothing in this case, variable is recreated in the loop for each element. Commented Dec 15, 2014 at 12:52
  • BenjaminToueg; Rusty : See last line of question Commented Dec 15, 2014 at 13:55

2 Answers 2

2
with open('file.txt') as f:
    # Remove the \n characters at the end of each line
    all_lines = [x.strip() for x in f.readlines()]
    for line in all_lines:
        # Check for presence of word' or word's 
        if line+"'" in all_lines or line+"'s" in all_lines:
            print('****')
        else:
            print(line)

It's worth noting that this is quite a brute force way of doing and for huge lists will take a bit longer (it loads the file into memory) but should give you an idea.

Sign up to request clarification or add additional context in comments.

Comments

1

you can use str.endswith:

text = open("list.txt", "r")

for line in text:
    test = line.strip()
    if test.endswith("'s"):
        line = "****" 

Here i have explained why your code is not going to work:

replace this:

test = line

to:

test = line.strip()   # to remove new line character

so your test will be rabbit\n', if you don't remove newline character

you also need to open file on read mode

text = open("list.txt",'r')

you match is not going to work, think of it:

suppose test="rabbit's"

    test+"'" will give you `rabbit's'`

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.