On 22.08.2012 08:21, Santosh Kumar wrote:
with open(givenfile) as file:
     # List to store the capitalised lines.
     lines = []
     for line in file:
         # Split words by spaces.
         words = line.split(' ')

The last element in your "words" list will still have a newline character appended to it.
You could probably use line.split().
See also the docs:
http://docs.python.org/py3k/library/stdtypes.html#str.split

         for i, word in enumerate(words):
             if len(word.strip(punctuation)) > 3:
                 # Capitalise and replace words longer than 3 (without
punctuation)
                 words[i] = word.capitalize()
         # Join the capitalised words with spaces.
         lines.append(' '.join(words))

This rebuilds the line including a newline character at the end.

     # Join the capitalised lines by the line separator
     capitalised = linesep.join(lines)

Because you haven't removed the newline character from each line, joining them with "linesep" introduces a second newline character after each line.

Bye, Andreas
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to