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


Groups > comp.lang.python > #27607

help me debug my "word capitalizer" script

Date 2012-08-22 11:51 +0530
Subject help me debug my "word capitalizer" script
From Santosh Kumar <sntshkmr60@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.3631.1345616511.4697.python-list@python.org> (permalink)

Show all headers | View raw


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 everytime it processes a new line. (Please notice that I don't
want the output in an another file, I want it on screen).

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


Thread

help me debug my "word capitalizer" script Santosh Kumar <sntshkmr60@gmail.com> - 2012-08-22 11:51 +0530
  Re: help me debug my "word capitalizer" script Hans Mulder <hansmu@xs4all.nl> - 2012-08-22 10:20 +0200
    Re: help me debug my "word capitalizer" script MRAB <python@mrabarnett.plus.com> - 2012-08-22 16:40 +0100

csiph-web