Would like to be able to replace every nth word in a text file with a blank space. I'm sure python is so capable.

that's simple

txt = "hello world hello world hello world hello world"

def replace_nth_word(n, text):
    text = text.split()
    j = 0
    for i in range(len(text)):
        j += 1
        if j == n:
            j = 0
            text[i] = " "

    return " ".join(text)

print replace_nth_word(3, txt)

""" output:
hello world   world hello   hello world
"""

now you can just open the file and read it and use this function.

Would like to be able to replace every nth word in a text file with a blank space. I'm sure python is so capable.

If this is homework, please show at least a little effort!

WOW, thank you all for your lightning-fast contributions.

I promised I did read a lot of posts and threads -thanks to which I have come across wonderful pieces of code, but nothing came so close as ultimatebuster's answer. Yes, man, you got it!

And not, it's not homework, i will use it to create cloze tests for my pupils. Being a teacher of english, this kind of tools are used almost everyday, so in a way it will create homework :-)

A couple of further suggestions to improve it:

1. could it open and read an external textfile e.g. clozetext.txt?
2. could the output of the program be appended to the original file clozetext.txt, as well as the list of all the words blanked out?

You all are great!!

This thread is marked solved, but here is built in slice version and cleaned up version of ultimatebuster's code (calling commented out, changed to return list of words to be same format with slice solution).

filetoread = 'clozebase.txt'

filetoappend = 'clozetest.txt'
appendf=open(filetoappend,'a')

everynth = 3

def replace_nth_word(n, text):
    ## cleaned up by Tony Veijalainen
    words = text.split() ## takes out newlines!!
    for j,word in enumerate(words):
        if not j % n:
            words[j] = "_"*8
    return words

textwords =  open(filetoread).read().split() ## takes out newlines!

## built in alternative
textwords[::everynth] = [8*'_'] * len(textwords[::everynth])

## separate function based
## textwords =  replace_nth_word(everynth,open(filetoread).read())

appendf.write(' '.join(textwords)+'\n')

# cleaned up version of ultimatebuster function 
# and my version samples from beginning to check correctness
print replace_nth_word(everynth,open(filetoread).read())[:10]
print textwords[:10]

Edited 6 Years Ago by pyTony: n/a

Wow, that was great.
A little push further? Could the program append the words blanked out at the end of the text? and Could it be forced not to blank out the 1st word??

Thanx, dude.

Like this, but this still mangles the punctuation and newlines like before:

filetoread = 'clozebase.txt'

filetoappend = 'clozetest.txt'

first = 1
everynth = 3

textwords =  open(filetoread).read().split() ## takes out newlines!

## built in alternative
replaced,textwords[first::everynth] = textwords[first::everynth],[8*'_'] * len(textwords[::everynth]),

open(filetoappend,'a').write(' '.join(textwords)+
                             '\n'+'_'*80+'\n'+
                             '\t'.join(replaced)+
                             '\n'*2)

print open(filetoappend).read()

It should be possible from this saving of values actually prepare other copy of the list with only letters replaced with lines (even right number one for each letter) and keep the punctuation and white space. This I have now however to offer.

EDIT: Here is that version:

import string
filetoread = 'clozebase.txt'
filetoappend = 'clozetest.txt'

first = 1
everynth = 3

textwords =  open(filetoread).read().split(' ')

## built in alternative
replaced = textwords[first::everynth]
textwords[first::everynth] = (''.join('_' if letter in string.letters else letter
                                      for letter in word)
                              for word in replaced
                              )

open(filetoappend,'a').write(' '.join(textwords)+
                             '\n'+'_'*80+'\n'+
                             '\t'.join(replaced)+
                             '\n'*2)

print open(filetoappend).read()

Edited 6 Years Ago by pyTony: n/a

The last code has problem that it zeroes two words at the end of line as it joins the two lines together to keep the new lines. First code removes the newlines. Fixing those faults is possible but bit complicated. Second version could be fixed by moving the routine to transform letters to underscores to function which stops when newline is found, then also the rhytm of zeroing is off in other direction.

First code better version here (should keep rhythm and newlines):

filetoread = 'clozebase.txt'
outfile = 'clozetest.txt'

first = 1
everynth = 3
neverintext='zzzzz'

textwords =  open(filetoread).read().replace('\n',neverintext+' ').split(' ') ## takes out newlines!

## save replacing words without neverintext
replaced = [word.replace(neverintext,'') for word in textwords[first::everynth]]

## zero word, add '\n' if neverintext in word
textwords[first::everynth] = [8*'_'+'\n' if neverintext in word else 8*'_'
                              for word in textwords[first::everynth]]

## restore newlines in other words
textwords = [word.replace(neverintext,'\n') for word in textwords]

open(outfile,'w').write(' '.join(textwords)+
                             '\n'+'_'*80+'\n'+
                             '\t'.join(replaced)+
                             '\n'*2)

print open(outfile).read()
This question has already been answered. Start a new discussion instead.