title: How to count and print the number of words in each line in a file?
body: <p>I want to count the total no of words in each line in a file and print them.I tried</p>
  
  <pre><code>with codecs.open('v.out','r',encoding='utf-8') as f:
      for line in f.readlines():
          words = len(line.strip(' '))
          print words
  </code></pre>
  
  <p>the input file is:</p>
  
  <pre><code>hello 
  try it
  who knows
  it may work
  </code></pre>
  
  <p>the output that I get is:</p>
  
  <pre><code>6
  7
  10
  12
  </code></pre>
  
  <p>but what I need is:</p>
  
  <pre><code>1 
  2 
  2
  3
  </code></pre>
  
  <p>is there any function that I can use? I have to print the first word of each line in a file, and similarly print the middle word and the last word of the line into separate files.</p>