help me debug my "word capitalizer" script

Andreas Perstinger andipersti at gmail.com
Wed Aug 22 03:42:23 EDT 2012


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



More information about the Python-list mailing list