Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #27614

Re: help me debug my "word capitalizer" script

Date 2012-08-22 09:42 +0200
From Andreas Perstinger <andipersti@gmail.com>
Subject Re: help me debug my "word capitalizer" script
References <CAE7MaQbrdVbbFWDJ8GHuoybT2SNPfZyyMFU_beBc_zMKpBHPFg@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.3637.1345621352.4697.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: help me debug my "word capitalizer" script Andreas Perstinger <andipersti@gmail.com> - 2012-08-22 09:42 +0200

csiph-web