Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '16,': 0.03; 'args': 0.04; 'error:': 0.05; 'sys': 0.05; 'parser': 0.07; 'valueerror:': 0.07; 'expected.': 0.09; 'script,': 0.09; 'throws': 0.09; 'unexpected': 0.09; 'unpack': 0.09; 'valueerror': 0.09; 'cc:addr:python-list': 0.10; 'argparse': 0.16; 'capitalizes': 0.16; 'subject:whitespaces': 0.16; 'to:addr:web.de': 0.16; 'wrote:': 0.17; 'input': 0.18; 'skip:" 30': 0.20; 'import': 0.21; 'cc:2**0': 0.23; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; '(most': 0.27; 'right.': 0.27; 'replace': 0.27; 'message- id:@mail.gmail.com': 0.27; 'kumar': 0.29; 'leaves': 0.29; 'methods.': 0.29; 'code': 0.31; 'file': 0.32; 'print': 0.32; 'traceback': 0.33; 'received:google.com': 0.34; 'subject:?': 0.35; 'received:209.85': 0.35; 'something': 0.35; 'but': 0.36; 'skip:p 20': 0.36; 'received:209': 0.37; 'subject:: ': 0.38; 'mean': 0.38; 'skip:l 20': 0.38; 'received:209.85.214': 0.39; 'takes': 0.39; 'your': 0.60; 'first': 0.61; 'more': 0.63; 'otten': 0.84; 'subject:Any': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:cc:content-type; bh=uB5MltJlIK/nj0A7fApBkclmJkPaCjdiKR0438Omtxo=; b=yhGXFHm8Ms4YV9rALqAaur2gAj7rnFdj6UVePe2Qpg6kICxf4BvLabri+KlLC3r5Ay O1Cmau7g6xHV78ojRPkiPEkQiINqJz7QuHqyBcWqWW6YwKM6ar3Wm7est4g36WBUAKL5 Ql+MJ4vvo2OMavrunoa5pmfJmvg1Lgv+XnpdGYts/ORdtnGQ0LIRKZyrnKLAMaByV+1f hoh18e1AkkfC54kpTA39qRs7Xb9JkvKw455zG2N5ZIdvphfbjDXSMikW6+Km15ZT7NIt RT5+LWJljbgpDSVfu3LSoCn7hBRFt6i7YismPFpiA7KnydwBL7/WuhyLrSqc9zrTsLyd UYwQ== MIME-Version: 1.0 X-Received: by 10.182.194.2 with SMTP id hs2mr856371obc.97.1359019164881; Thu, 24 Jan 2013 01:19:24 -0800 (PST) In-Reply-To: References: <50FAEE23.5030107@lightbird.net> <50FFBCC5.5020506@davea.name> Date: Thu, 24 Jan 2013 14:49:24 +0530 Subject: Re: Any algorithm to preserve whitespaces? From: Santosh Kumar To: Peter Otten <__peter__@web.de> Content-Type: text/plain; charset=UTF-8 Cc: python-list X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 40 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1359019167 news.xs4all.nl 6886 [2001:888:2000:d::a6]:49017 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:37545 On 1/24/13, Peter Otten <__peter__@web.de> wrote: > Santosh Kumar wrote: > >> Yes, Peter got it right. >> >> Now, how can I replace: >> >> script, givenfile = argv >> >> with something better that takes argv[1] as input file as well as >> reads input from stdin. >> >> By input from stdin, I mean that currently when I do `cat foo.txt | >> capitalizr` it throws a ValueError error: >> >> Traceback (most recent call last): >> File "/home/santosh/bin/capitalizr", line 16, in >> script, givenfile = argv >> ValueError: need more than 1 value to unpack >> >> I want both input methods. > > You can use argparse and its FileType: > > import argparse > import sys > > parser = argparse.ArgumentParser() > parser.add_argument("infile", type=argparse.FileType("r"), nargs="?", > default=sys.stdin) > args = parser.parse_args() > > for line in args.infile: > print line.strip().title() # replace with your code > This works file when I do `script.py inputfile.txt`; capitalizes as expected. But it work unexpected if I do `cat inputfile.txt | script.py`; leaves the first word of each line and then capitalizes remaining.