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

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

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

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. Ahmed Moustafa
    Replies:
    0
    Views:
    972
    Ahmed Moustafa
    Nov 15, 2003
  2. Joona I Palaste
    Replies:
    1
    Views:
    572
    Berco
    Jan 7, 2004
  3. Berco
    Replies:
    0
    Views:
    1,827
    Berco
    Jan 7, 2004
  4. Bapaiah Katepalli
    Replies:
    1
    Views:
    1,717
    Mike Treseler
    Jun 23, 2006
  5. Grzegorz Chrupala
    Replies:
    2
    Views:
    362
    Grzegorz Chrupala
    Jun 30, 2003
  6. Michelle Pace
    Replies:
    9
    Views:
    250
    Joel VanderWerf
    Apr 26, 2011
  7. Ptaku25
    Replies:
    0
    Views:
    172
    Ptaku25
    Dec 28, 2005
  8. Santosh Kumar

    Any algorithm to preserve whitespaces?

    Santosh Kumar, Jan 19, 2013, in forum: Python
    Replies:
    0
    Views:
    203
    Santosh Kumar
    Jan 19, 2013
Loading...