Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.freenet.ag!news2.euro.net!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; 'valueerror:': 0.07; 'python': 0.09; 'expected.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'script,': 0.09; 'throws': 0.09; 'unexpected': 0.09; 'unpack': 0.09; 'valueerror': 0.09; 'alpha': 0.15; 'argparse': 0.16; 'capitalizes': 0.16; 'gamma': 0.16; 'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; 'reproduce': 0.16; 'subject:whitespaces': 0.16; 'wrote:': 0.17; '>>>': 0.18; 'input': 0.18; 'skip:" 30': 0.20; 'import': 0.21; 'delta': 0.22; 'header:User-Agent:1': 0.26; '(most': 0.27; 'right.': 0.27; 'skip:# 10': 0.27; 'replace': 0.27; 'header:X -Complaints-To:1': 0.28; 'cat': 0.29; 'kumar': 0.29; 'leaves': 0.29; 'methods.': 0.29; 'code': 0.31; 'file': 0.32; 'print': 0.32; 'traceback': 0.33; 'to:addr:python-list': 0.33; 'subject:?': 0.35; 'something': 0.35; 'received:org': 0.36; 'but': 0.36; 'skip:p 20': 0.36; 'subject:: ': 0.38; 'mean': 0.38; 'skip:l 20': 0.38; 'to:addr:python.org': 0.39; 'takes': 0.39; 'header:Received:5': 0.40; 'your': 0.60; 'first': 0.61; 'more': 0.63; 'otten': 0.84; 'subject:Any': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Any algorithm to preserve whitespaces? Date: Thu, 24 Jan 2013 10:30:54 +0100 Organization: None References: <50FAEE23.5030107@lightbird.net> <50FFBCC5.5020506@davea.name> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p50849719.dip.t-dialin.net User-Agent: KNode/4.7.3 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: 71 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1359019851 news.xs4all.nl 6904 [2001:888:2000:d::a6]:51849 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:37546 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