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


Groups > comp.lang.python > #27614 > unrolled thread

Re: help me debug my "word capitalizer" script

Started byAndreas Perstinger <andipersti@gmail.com>
First post2012-08-22 09:42 +0200
Last post2012-08-22 09:42 +0200
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

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

#27614 — Re: help me debug my "word capitalizer" script

FromAndreas Perstinger <andipersti@gmail.com>
Date2012-08-22 09:42 +0200
SubjectRe: help me debug my "word capitalizer" script
Message-ID<mailman.3637.1345621352.4697.python-list@python.org>
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

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web