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


Groups > comp.lang.python > #37549 > unrolled thread

Re: Any algorithm to preserve whitespaces?

Started bySantosh Kumar <sntshkmr60@gmail.com>
First post2013-01-24 15:39 +0530
Last post2013-01-24 15:39 +0530
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Any algorithm to preserve whitespaces? Santosh Kumar <sntshkmr60@gmail.com> - 2013-01-24 15:39 +0530

#37549 — Re: Any algorithm to preserve whitespaces?

FromSantosh Kumar <sntshkmr60@gmail.com>
Date2013-01-24 15:39 +0530
SubjectRe: Any algorithm to preserve whitespaces?
Message-ID<mailman.951.1359022158.2939.python-list@python.org>
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 <module>
>>>>         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
>

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web