Hello, I am new to daniWeb. I am creating a script but have run into a problem when it comes to printing the line number.

   for word in words:
      word = clearup(word)
      if word in dictionary:
         print word

On the last line I want to do something like... print word':'linenumber Would I use something from " from linecache import getline "

I have tried:

linenum = getline(filepath, ##line_number##)
         print word.strip('.')
         print ' :(',
         print linenum
         print ')'

It doesn't work and prints Either data about a line of python code (not line number from the text file) or just prints a bunch of Trues... could someone please explain what I need to do?

If you don't want to read all of my nonsense just skip to the example.

Basically, I have a tuple of words. I am comparing it to a doc.txt to see if the word from my tuple match any of the words in my document. If item in my tuple that matches an item in the document I pass else: i print the item. I want it to print the line number of the txt document along with the word if it doesn't match.


My doc.txt has this in it:


my_tuple (



if word in dictionary:
         print word

that outputs ... uligy What I want it to output is: uligy : line 3

Enumerate is one way to do it.

for ctr, word in enumerate(words):
      word = clearup(word)
      if word not in dictionary:
         print "%s : line %d" % (word, ctr)

Thanks! I looked up enumerate and read about it. It was exactly what I needed.

This article has been dead for over six months. Start a new discussion instead.