Re: Any algorithm to preserve whitespaces?

Discussion in 'Python' started by Santosh Kumar, Jan 23, 2013.

  1. 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.
     
    Santosh Kumar, Jan 23, 2013
    #1
    1. Advertising

  2. 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
     
    Steven D'Aprano, Jan 24, 2013
    #2
    1. Advertising

  3. Santosh Kumar

    Dave Angel Guest

    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
     
    Dave Angel, Jan 24, 2013
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Santosh Kumar

    Any algorithm to preserve whitespaces?

    Santosh Kumar, Jan 19, 2013, in forum: Python
    Replies:
    0
    Views:
    145
    Santosh Kumar
    Jan 19, 2013
  2. Lie Ryan
    Replies:
    0
    Views:
    148
    Lie Ryan
    Jan 19, 2013
  3. Mitya Sirenef

    Re: Any algorithm to preserve whitespaces?

    Mitya Sirenef, Jan 19, 2013, in forum: Python
    Replies:
    0
    Views:
    145
    Mitya Sirenef
    Jan 19, 2013
  4. Santosh Kumar

    Re: Any algorithm to preserve whitespaces?

    Santosh Kumar, Jan 23, 2013, in forum: Python
    Replies:
    0
    Views:
    107
    Santosh Kumar
    Jan 23, 2013
  5. Peter Otten
    Replies:
    0
    Views:
    109
    Peter Otten
    Jan 23, 2013
Loading...

Share This Page