Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > using python with -c (as a inline execution in shell)

Reply
Thread Tools

using python with -c (as a inline execution in shell)

 
 
les ander
Guest
Posts: n/a
 
      11-16-2004
Hi,
in perl one can do all sorts of stuff in a single line form the command shell.
python has the -c option.
it is easy to do stuff like,
python -c "print 10+20"
etc statements
but suppose I want to read from the stdin (piped) i don't know how to do this since
python -c "from sys import stdin; for x in stdin: print x"

gives a syntax error.

Anyone one know how to do this?
Also, is there is site that talkes about doing this kind of stuff?
the man pages in xterm are pretty sparse and don't say that much
thanks
 
Reply With Quote
 
 
 
 
Skip Montanaro
Guest
Posts: n/a
 
      11-16-2004

les> python -c "from sys import stdin; for x in stdin: print x"

les> gives a syntax error.

les> Anyone one know how to do this?

How about:

% python -c 'from sys import stdin
> for x in stdin:
> print x
> '

hi there
bye
hi there

bye

Skip
 
Reply With Quote
 
 
 
 
Simon Brunning
Guest
Posts: n/a
 
      11-16-2004
On 16 Nov 2004 07:44:19 -0800, les ander <(E-Mail Removed)> wrote:
> but suppose I want to read from the stdin (piped) i don't know how to do this since
> python -c "from sys import stdin; for x in stdin: print x"
>
> gives a syntax error.


If you really must do this, there's always PyOne:
<http://www.unixuser.org/~euske/pyone/>.

Wouldn't touch it with a bargepole, myself.

--
Cheers,
Simon B,
http://www.velocityreviews.com/forums/(E-Mail Removed),
http://www.brunningonline.net/simon/blog/
 
Reply With Quote
 
Alex Martelli
Guest
Posts: n/a
 
      11-16-2004
les ander <(E-Mail Removed)> wrote:

> but suppose I want to read from the stdin (piped) i don't know how to do
> this since python -c "from sys import stdin; for x in stdin: print x"
>
> gives a syntax error.
>
> Anyone one know how to do this?


Not _good_ ways, but:

import sys; print sys.stdin.read()

import sys; sys.stdout.write(sys.stdin.read())

import sys; sys.stdout.writelines(sys.stdin)

are some approaches. None matches the double-spacing effect you appear
to be after, but changing every '\n' into two ain't _that_ hard, e.g.

import sys; print sys.stdin.read().replace('\n','\n\n')

etc, will give kinda the same doublespacing your approach would give if
it worked. Still, Python just isn't oneline-oriented...


Alex
 
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
Tool which expands implicitly inline inline functions tthunder@gmx.de C++ 3 06-16-2005 12:54 AM
To inline or not to inline? Alvin C++ 7 05-06-2005 03:04 PM
Function delcared inline but not defined inline Nish C Programming 4 10-08-2004 03:31 PM
External inline functions calling internal inline functions Daniel Vallstrom C Programming 2 11-21-2003 01:57 PM
inline or not to inline in C++ Abhi C++ 2 07-03-2003 12:07 AM



Advertisments