python - reading and checking the consecutive words in a file -
i want read words in file, , example, check if word "1",if word 1, have check if next word "two". after have other task. can u me check occurance of "1" , "two" consecutively.
i have used
filne = raw_input("name of existing file proceesed:") f = open(filne, 'r+') word in f.read().split(): in xrange(len(word)): print word[i] print word[i+1]
but not working.
the easiest way deal consecutive items zip
:
with open(filename, 'r') f: # better way open file line in f: # each line words = line.strip().split() # words on line word1, word2 in zip(words, words[1:]): # iterate through pairs if word1 == '1' , word2 == 'crore': # test pair
at moment, indices (i
, i+1
) within each word (i.e. characters) not words within list.
Comments
Post a Comment