1

I would like to delete some occurrences of the word "name" in a file but not others. I am guessing the best way to do this is to use some sort of accumulator pattern but I am not sure how to implement it.

So far I have:

f = open("old_text.txt")
number = f.read()
f.close

name_occurrence = (number.count("Courtney"))

I am just using 'Courtney' as an example of an actual name in the file. I would like to somehow delete every odd occurrence of the word "Courtney" but not the even ones i.e. as number.count iterates it assigns each instance of "Courtney" a number value and then some code deletes occurrences of the word "Courtney" that have a value of 1,3,5,7...

Thanks for your help,

Fluffy

Ben
  • 843
  • 2
  • 9
  • 20
  • What if you make a for loop to search through the strings, as in this answer: http://stackoverflow.com/questions/2768628/iterating-through-string-word-at-a-time-in-python?rq=1 Then you can keep track of whether you deleted the last name or not and choose to delete the one you're on – darthbith Oct 02 '13 at 12:37
  • Thank you for the link. It is helpful and I can see how it would be used but I think I am too early in my understanding of programming to implement this myself. I am probably trying to do something too complicated since I have only been working at programming/python for a few months. – Ben Oct 02 '13 at 12:43
  • Just try it! What's the worst that can happen? If you get errors, post the code you're trying and the error on here, and people will be happy to help you (after you do some research and try to fix it yourself, of course). As your question stands, it may be closed because its not specific enough... – darthbith Oct 02 '13 at 12:46

2 Answers2

1

Not tested, but you could try a regex like this:

import re

with open("old_text.txt") as f:
   txt = f.read()
   new_txt=re.sub(r'(\bCourtney\b.*?)(\s*\Courtney\b\s*)','\1',txt,re.S)

If you want a dynamic string (i.e., that has a variable in it):

import re

name='Courtney'

with open("old_text.txt") as f:
   txt = f.read()
   new_txt=re.sub(r'(\b{}\b.*?)(\s*\{}\b\s*)'.format(name,name),'\1',txt,re.S)
dawg
  • 98,345
  • 23
  • 131
  • 206
1

This is ugly, but it works and it's pure python

file names.txt (I've put numbers in front of name Courtney to be easier to fallow which ones are deleted):

11111 Courtney Emma Jessica 22222 Courtney Ashley Amanda Jennifer 
Sarah Michael 33333 Courtney Christopher Matthew Joshua David
Emma Jessica Ashley Amanda Jennifer 44444 Courtney 
Sarah 55555 Courtney Michael 66666 Courtney Christopher 
77777 Courtney Emma Jessica Ashley Amanda Jennifer 88888 Courtney 
Sarah Michael 99999 Courtney Christopher Matthew

code:

f = open("names.txt",'r')
splited_lines = []
name_occurrence = 0
name = "Courtney"

#create list of lines where line is list of words
index = 1
for line in f:
    name_occurrence += line.count(name)
    splited_line = line.split()
    splited_lines.append(splited_line)
f.close

#delete every even name (Courtney)
#if you want every odd to be deleted set word_counter on 0
word_counter = -1    
for i,line in enumerate(splited_lines):
    for j,word in enumerate(line):
        if (name in word):
            word_counter += 1 
            if (word_counter%2 == 0):
                splited_lines[i][j] = word.replace(name, "")

#create string to write back to file
text_to_save = ""
for line in splited_lines:
    for word in line:
        if word != "":
            text_to_save += word + " "
    text_to_save += "\n"

#write to file
with open('names.txt', 'w') as f:
    f.writelines(text_to_save)

I hope this helps. Feel free to ask if you don't understand something.

Aleksandar
  • 3,541
  • 4
  • 34
  • 57