Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Any algorithm to preserve whitespaces?

Reply
Thread Tools

Re: Any algorithm to preserve whitespaces?

 
 
Santosh Kumar
Guest
Posts: n/a
 
      01-23-2013
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.
 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      01-24-2013
Santosh Kumar wrote:

> Yes, Peter got it right.


Peter? Which Peter? What's "it" that he got right?

You have deleted all context from your post, so I have no idea what you are
talking about. And whatever program you are using to post is stripping out
threading information, so I can't tell what post you are replying to.

Please take careful note of the posting conventions used by the experienced
regulars on this forum, and copy their style. That is for your benefit as
well as ours.


> 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.


The usual convention in Unix and Linux is that if the file name is "-", read
from stdin instead. Something like this, untested:


givenfile = sys.argv[1]
if givenfile == '-':
data = sys.stdin.read()
else:
data = open(givenfile).read()


Adding error checking etc. is left as an exercise.




--
Steven

 
Reply With Quote
 
 
 
 
Dave Angel
Guest
Posts: n/a
 
      01-24-2013
On 01/23/2013 07:49 PM, Steven D'Aprano wrote:
> Santosh Kumar wrote:
>
>> Yes, Peter got it right.

>
> Peter? Which Peter? What's "it" that he got right?
>
> You have deleted all context from your post, so I have no idea what you are
> talking about.


Right.

> And whatever program you are using to post is stripping out
> threading information, so I can't tell what post you are replying to.


You're not entirely right here. Santosh's message threads correctly to
mine when I look with Thunderbird. And mine is parallel to one by Peter
Otten, who suggested rstrip() to get rid of the extra newline. About
10% of your posts show up as top-level (starting new threads), even
though I know you're careful. So there seem to be more than one
threading protocol, and the multiple protocols are fighting each other.
I'd love to see a spec that I could use to (manually?) check whether
the threads are right or not.

the relevant timestamps (at least as seen from USA EST zone) are
Santosh at 4:20 am
Peter Otten at 4:46 am
DaveA at 5:34 am
Santosh at 9:56 am
Steven D'Aprano at 7:49 pm

But your message was a reply to Santosh's 9:56 am message.

(I'm deleting the rest, because I'm not responding to the commandline
parsing question)




--
DaveA
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Any algorithm to preserve whitespaces? Santosh Kumar Python 0 01-23-2013 09:20 AM
Re: Any algorithm to preserve whitespaces? Mitya Sirenef Python 0 01-19-2013 07:04 PM
Re: Any algorithm to preserve whitespaces? Lie Ryan Python 0 01-19-2013 10:26 AM
Any algorithm to preserve whitespaces? Santosh Kumar Python 0 01-19-2013 10:13 AM
FileUtils - :preserve does not preserve mtime of directories on Windoze Grzegorz Chrupala Ruby 2 06-30-2003 07:38 AM



Advertisments