How to print the incorrect words and line number from text file?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lightning18
    New Member
    • May 2010
    • 16

    How to print the incorrect words and line number from text file?

    I have a list of incorrect words called # words[] and another list containing my txt file # text[].
    I want to print the line number of the words located in the text.

    I get the following error for my code.
    invalid syntax: print(text, d[counter])

    I am trying to print the words with its line number located in the text but it is not working its only printing the words anyone know why?

    Below is the last bit of code start from d = {}
    Code:
    d = {}
    counter = 0
    for lines in words:
        text = lines.split()
        counter += 1
    if text not in d:
        d[text] = [counter]
    if text in d:
        d[text.append[counter]
          print(text, d[counter])
    Last edited by Niheel; May 24 '10, 05:00 AM. Reason: Please use proper punctuation and grammar. It will make answering your questions a lot easier.
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by lightning18
    I have a list of incorrect words called # words[] and another list containing my txt file # text[].
    I want to print the line number of the words located in the text.

    I get the following error for my code.
    invalid syntax: print(text, d[counter])

    I am trying to print the words with its line number located in the text but it is not working its only printing the words anyone know why?

    Below is the last bit of code start from d = {}
    Code:
    d = {}
    counter = 0
    for lines in words:
        text = lines.split()
        counter += 1
    if text not in d:
        d[text] = [counter]
    if text in d:
        d[text.append[counter]
          print(text, d[counter])
    Your for loop does not do anything. I think your if statements (should be an if/elif block) should be indented to the same level as the for loop. Variable text is a list. You cannot use a list object as a dictionary key because it is mutable. I don't think you meant to do that anyway

    Show us a sample of your data and a sample of your required output, and we can make some recommendations .

    Comment

    • Glenton
      Recognized Expert Contributor
      • Nov 2008
      • 391

      #3
      Often with invalid syntax it's the line before that's at fault.

      In this case your line 9 needs at extra ]

      Comment

      • dwblas
        Recognized Expert Contributor
        • May 2008
        • 626

        #4
        And your indentation is off.

        We usually add keys to a dictionary in the following manner, eliminating the second if() statement (which should be an else BTW).
        Code:
        if next_word not in d:
            d[next_word] = []     ## add new key pointing to an empty list
        d[next_word].append(counter)

        Comment

        • Glenton
          Recognized Expert Contributor
          • Nov 2008
          • 391

          #5
          Originally posted by lightning18
          I have a list of incorrect words called # words[] and another list containing my txt file # text[].
          I want to print the line number of the words located in the text.

          I get the following error for my code.
          invalid syntax: print(text, d[counter])

          I am trying to print the words with its line number located in the text but it is not working its only printing the words anyone know why?

          Below is the last bit of code start from d = {}
          Code:
          d = {}
          counter = 0
          for lines in words:
              text = lines.split()
              counter += 1
          if text not in d:
              d[text] = [counter]
          if text in d:
              d[text.append[counter]
                print(text, d[counter])
          Actually using defaultdict from the collections module is perfect for this kind of thing. I seem to remember doing this in a previous post? Was that for you?

          Anyway it's something like this:
          Code:
          from collections import defaultdict
          
          d=defaultdict(list)
          counter = 0
          for lines in words:
              text = lines.split()
              counter += 1
              d[text].append(counter)
          for t in d:
              print t,d[t]
          Mmm. Doing this made me realise just how many errors there were in your original code! e.g. d[counter] is not defined.

          Comment

          Working...