remove every nth word from file
The function should return the rest of the words from the file, just not the nth words. I've got the opening, reading and closing the text file down, but I've been struggling to figure out the rest. I tried using append but quickly realized that's n开发者_运维百科ot the way to go about it.
My failed coding so far:
def message(fname, n):
infile = open(fname,'r')
mess = infile.read()
infile.close()
mess.append('')
return mess
So it should return the rest of the words from the file, just not the nth words.
To remove every nth word, the pseudocode is to read every word and write them all out except the ones where wordnumber modulo n
is equal to 0.
In other words:
nth = 7
wordnumber = 0
while not end-of-file:
read word
wordnumber = wordnumber + 1
if wordnumber % nth is not 0:
write word
That's it really. But don't make the mistake of thinking that's Python. My pseudo-code looks remarkably like Python since that language is ideal for the use, but you can't just plug it into a Python interpreter and expect it to work as is.
Of course, it shouldn't be too hard to adapt it into Python, or any other normal language for that matter (normal in the sense of languages with while
and if
statement, not those ones which are more declarative in nature).
One could read the entire file into a list and use the del statement to remove every n-th item.
def remove_every_nth_word_from_file(filename, n):
with open(filename) as f:
words = f.read().split()
del words[n - 1::n]
return words
f.read() function reads the entire file as a string; split() function splits this string by whitespace; words[n - 1::n] is a list slice that says, starting at (n - 1)-th position, include every n-th item; del statement removes this slice from the list.
def all_but_n(file,num):
rf=[] #rf is the list containing words for the returned file
n=num
f=open(file,'r')
i=0#for indexing the file
for line in f:
line=line.strip('\n').split(' ')
for word in line:
if n!=i+1:
rf.append(word)
else:
n=n+num
i=i+1
return rf
all_but_n('',3)
You could most certainly use list comprehensions to improve the speed. I wrote all_but_n() in this manner, so that you could understand what's happening
精彩评论