help me debug my "word capitalizer" script

Hans Mulder hansmu at xs4all.nl
Wed Aug 22 04:20:57 EDT 2012


On 22/08/12 08:21:47, Santosh Kumar wrote:
> Here is the script I am using:
> 
> from os import linesep
> from string import punctuation
> from sys import argv
> 
> script, givenfile = argv
> 
> with open(givenfile) as file:
>     # List to store the capitalised lines.
>     lines = []
>     for line in file:
>         # Split words by spaces.
>         words = line.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))
>     # Join the capitalised lines by the line separator
>     capitalised = linesep.join(lines)
> # Optionally, write the capitalised words back to the file.
> 
> print(capitalised)
> 
> 
> Purpose of the script:
> To capitalize the first letter of any word in a given file, leaving
> words which have 3 or less letters.
> 
> Bugs:
> I know it has many bugs or/and it can be improved by cutting down the
> code, but my current focus is to fix this bug:
>   1. When I pass it any file, it does it stuff but inserts a blank
> line every time it processes a new line. (Please notice that I don't
> want the output in an another file, I want it on screen).

The lines you read from your input file end in a line separator.
When you print them, the 'print' command adds another line separator.
This results in two line separators in a row, in other words, a blank
line.

The best way to solve this is usually to remove the line separator
right after you've read in the line.  You could do that by inserting
after line 10:

        line = line.rstrip()

That will remove all whitespace characters (spaces, tabs, carriage
returns, newlines) from the end of the line.

Alternatively, if you want to remove only the line separator,
you could do:

        if line.endswith(linesep):
            line = line[:-len(linesep)]

The 'if' command is only necessary for the last line, which may or
may not end in a linesep.  All earlier lines are guaranteed to end
with a linesep.


Hope this helps,

-- HansM



More information about the Python-list mailing list