Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder2.hal-mli.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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; 'see:': 0.07; 'valueerror:': 0.07; 'python': 0.09; 'expected.': 0.09; 'script,': 0.09; 'throws': 0.09; 'unexpected': 0.09; 'unpack': 0.09; 'valueerror': 0.09; 'cc:addr:python-list': 0.10; 'alpha': 0.15; 'argparse': 0.16; 'capitalizes': 0.16; 'gamma': 0.16; 'reproduce': 0.16; 'subject:whitespaces': 0.16; 'to:addr:web.de': 0.16; 'wrote:': 0.17; '>>>': 0.18; 'input': 0.18; 'skip:" 30': 0.20; 'import': 0.21; 'delta': 0.22; 'cc:2**0': 0.23; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply- To:1': 0.25; '(most': 0.27; 'right.': 0.27; 'skip:# 10': 0.27; 'replace': 0.27; 'message-id:@mail.gmail.com': 0.27; '>>>>': 0.29; 'cat': 0.29; 'kumar': 0.29; 'leaves': 0.29; 'methods.': 0.29; 'url:mailman': 0.29; 'code': 0.31; 'url:python': 0.32; 'file': 0.32; 'print': 0.32; 'url:listinfo': 0.32; 'traceback': 0.33; 'received:google.com': 0.34; 'subject:?': 0.35; 'received:209.85': 0.35; 'something': 0.35; 'but': 0.36; 'url:org': 0.36; 'skip:p 20': 0.36; 'received:209': 0.37; 'subject:: ': 0.38; 'mean': 0.38; 'skip:l 20': 0.38; 'takes': 0.39; 'url:mail': 0.40; '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=RQ+cY/XAqvdil4ZUQOwOSgLdJSoRghRPgxPhqskcWGE=; b=IL6/GAd11IffPDSij60kv7qVSdkSUU/PKdWXcn3vUUkIY+wIPCzhGWy59fBLVoToXh fCHH3syDRhXGuoIBi+Z/XaXP70tk4hwbHOL3ObrCTvpTTg3LnA8cfeIRqQJks9a1r7wz Du1QLaMRKJjLWzwTpM6DI8sRlYdIOo/ZCO87wlNSo5cRTjo8ugSpHtDKf0hO/iGf6I8x jlm97ZoUCkColfu9XQet+R3+n/UmahHrUtZ8KYH0oVbl9LU5XKgP48c6ImZEF/F3AZgn yXgt7oVkgbWa3nByngIlTjbHtqrsh0SZny9igmMl242iBRTWOs1baBX5G/4XJ+uW4v7H /xag== MIME-Version: 1.0 X-Received: by 10.60.12.74 with SMTP id w10mr997126oeb.100.1359022150217; Thu, 24 Jan 2013 02:09:10 -0800 (PST) In-Reply-To: References: <50FAEE23.5030107@lightbird.net> <50FFBCC5.5020506@davea.name> Date: Thu, 24 Jan 2013 15:39:09 +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@python.org 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: 77 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1359022158 news.xs4all.nl 6928 [2001:888:2000:d::a6]:60468 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:37549 But I can; see: http://pastebin.com/ZGGeZ71r On 1/24/13, Peter Otten <__peter__@web.de> wrote: > Santosh Kumar wrote: > >> 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. > > I cannot reproduce that: > > $ cat title.py > #!/usr/bin/env python > 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 > $ cat inputfile.txt > alpha beta > gamma delta epsilon > zeta > $ cat inputfile.txt | ./title.py > Alpha Beta > Gamma Delta Epsilon > Zeta > $ ./title.py inputfile.txt > Alpha Beta > Gamma Delta Epsilon > Zeta > > > -- > http://mail.python.org/mailman/listinfo/python-list >