Hi all,
I would like to use python for a replacement for some binutils. I would
like to be able to pipe things into python. Actually I would not like
writing a 'script' to handle the input, but write the python commands
right onto the command line.
It would be possible to use here-documents (for having the correct
identation during e.g while loops) for writing python code on
the fly, but then how and where to get the pipe output:
echo -e 'line1\nline2' | python <<EOF ....
Does not really work...
Would anybody have an idea how to use python like e.g. awk etc. As a
tool within, where I can handle the stdin data, and write the code right
there (e.g. with here documents)?
thanks a lot
calmar
--
calmar
(o_ It rocks: LINUX + Command-Line-Interface
//\
V_/_ http://www.calmar.ws 6 2518
Hi,
This is from C:\Python23\Lib \site-packages\win32\ Demos\pipes\cat .py:
'''cat.py
a version of unix cat, tweaked to show off runproc.py
'''
import sys
data = sys.stdin.read( 1)
sys.stdout.writ e(data)
sys.stdout.flus h()
while data:
data = sys.stdin.read( 1)
sys.stdout.writ e(data)
sys.stdout.flus h()
# Just here to have something to read from stderr.
sys.stderr.writ e("Blah...")
# end of cat.py
-pekka-
calmar wrote: Hi all,
I would like to use python for a replacement for some binutils. I would like to be able to pipe things into python. Actually I would not like writing a 'script' to handle the input, but write the python commands right onto the command line.
It would be possible to use here-documents (for having the correct identation during e.g while loops) for writing python code on the fly, but then how and where to get the pipe output:
echo -e 'line1\nline2' | python <<EOF ....
Does not really work...
Would anybody have an idea how to use python like e.g. awk etc. As a tool within, where I can handle the stdin data, and write the code right there (e.g. with here documents)?
thanks a lot calmar
On Sun, 14 Nov 2004 18:12:42 +0100, calmar wrote: It would be possible to use here-documents (for having the correct identation during e.g while loops) for writing python code on the fly, but then how and where to get the pipe output:
echo -e 'line1\nline2' | python <<EOF ....
Does not really work...
Would anybody have an idea how to use python like e.g. awk etc. As a tool within, where I can handle the stdin data, and write the code right there (e.g. with here documents)?
Can you post a working example for awk? I'll admit I'm not a shell expert,
but it seems like you are trying to stuff two files onto stdin, the "echo"
result and the python program. I don't understand how that could ever
work, and now you've piqued my curiosity.
calmar <ca****@calmar. ws> wrote: I would like to be able to pipe things into python.
Python is perfectly happy reading stdin and writing to stdout, so it's
also perfectly happy acting as a component in a pipe.
Actually I would not like writing a 'script' to handle the input, but write the python commands right onto the command line.
You can do this with the -c command line option:
$ python -c 'print "hello world"'
hello world
It would be possible to use here-documents (for having the correct identation during e.g while loops) for writing python code on the fly, but then how and where to get the pipe output:
I think you're confusing two different things: where python gets the
code it's going to execute, and where it gets the input it's going to
feed to that executing code. You can use here-is documents for the
input, and -c for the code, and end up with something like this:
$ python -c 'import sys for line in sys.stdin: print line[:10]
' << EOF line one of many line two of many line three of many EOF
line one o
line two o
line three
Whether or not the single-quote mechanism reads past newlines and
properly preserves indentation is more a function of what shell you're
using than the python interpreter itself (the above was done with bash).
Would anybody have an idea how to use python like e.g. awk etc. As a tool within, where I can handle the stdin data, and write the code right there (e.g. with here documents)?
Well, the above will work, but I don't think it's a particularly good
idea. I think you would do better putting the text of your python
script in a .py file, make the first line #!/usr/bin/python, and make it
executable. Then you've got a stand-alone program that you can edit and
maintain apart from the shell script that surrounds it. In the long
run, this is likely to be simpler to test and to maintain.
Of course, if you want to be a bit perverse, you can always do:
$ python /dev/stdin << EOF print "hello, bizarre world" EOF
hello, bizarre world
but I think that's more of a curiosity than a useful tool. Along the
same lines, I once knew somebody who linked /dev/tty.c to /dev/tty. He
could then do "cc /dev/tty.c" and type in c code directly to the
compiler". He was a sick puppy.
On 2004-11-14, Roy Smith <ro*@panix.co m> wrote:
hi, $ python -c 'import sys for line in sys.stdin: print line[:10]
' << EOF line one of many line two of many line three of many EOF line one o line two o line three
Yeah, that works nicely.
Actually, I didn't find the solution to just press enter (after a -c
'..) and write the idented code I want.
executable. Then you've got a stand-alone program that you can edit and maintain apart from the shell script that surrounds it. In the long run, this is likely to be simpler to test and to maintain.
Yeah, true.
Thanks a lot to all!
--
calmar
(o_ It rocks: LINUX + Command-Line-Interface
//\
V_/_ http://www.calmar.ws
On 2004-11-14, Jeremy Bowers <je**@jerf.or g> wrote:
hi, Can you post a working example for awk? I'll admit I'm not a shell expert, but it seems like you are trying to stuff two files onto stdin, the "echo" result and the python program. I don't understand how that could ever work, and now you've piqued my curiosity.
Well, it would be something like (easy example):
echo -e 'line1\nline2\n line3' | python -c '
import sys
for line in sys.stdin:
print line'
That works now (with bash). I get the text into python and can do there
whatever I want. Awk/Sed/grep or whatever kind of tasks.
My problem, I didn't find the solution to just write the code after the
-c '...' including newlines. Sooo easy, but I didn't find that solution
(not even after hours doing curious stuff:)
Cheers
calmar
PS: would there be a smarter way to get the
stdin lines into a loop?
--
calmar
(o_ It rocks: LINUX + Command-Line-Interface
//\
V_/_ http://www.calmar.ws
calmar <ca****@calmar. ws> wrote: echo -e 'line1\nline2\n line3' | python -c ' import sys for line in sys.stdin: print line'
The fileinput module is useful, it reads from stdin or from the files
provided on the command line (sys.argv), eg
$ python -c '
import fileinput, sys
for line in fileinput.input ():
# Your code to manipulate line here
sys.stdout.writ e(line)
'
This is a line
This is another line
This is a line
This is another line
(The above is (almost) equivalent to perl -pe '# Your code here' BTW
which was in itself derived from the awk usage I think. The almost
comes in when stdin is from the terminal - you need to press CTRL-D
twice for the python version and once for the perl version)
--
Nick Craig-Wood <ni**@craig-wood.com> -- http://www.craig-wood.com/nick This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Kay Lee |
last post by:
Hi,
I looked up os module to find out some method to move and copy files in
python,
but os doesn't support such methods.
Is there any way to move & copy files in python?
Thanks in adv.
|
by: Thomas Reichelt |
last post by:
Moin,
short question: is there any language combining the syntax, flexibility and
great programming experience of Python with static typing? Is there a
project to add static typing to Python?
Thank you,
--
greetz tom
|
by: Ashot |
last post by:
This is sort of both Python and Vim related (which is why I've posted to
both newsgroups).
Python related:
----------------------
I have been frustrated for quite some time with a lack of a history
command in IDLE (in fact with IDLE in general). Often I'll develop new
code at the command line, testing each line as I go. Currently I have to
copy and paste, removing outputs and the ">>>" at each line.
Is it perhaps possible to make...
|
by: Xah Lee |
last post by:
http://python.org/doc/2.4.1/lib/module-re.html
http://python.org/doc/2.4.1/lib/node114.html
---------
QUOTE
The module defines several functions, constants, and an exception. Some
of the functions are simplified versions of the full featured methods
for compiled regular expressions. Most non-trivial applications always
use the compiled form
UNQUOTE
|
by: tnoell |
last post by:
Hi comp.lang.python:
New to the group and new to python, so don't tear me up too much ...
I installed the GNU readline support in python2.4, and it is working,
but there is one annoying behaviour that I am hoping to squash ...
Namely, when I hit <esc> to go to edit mode, then hit 'k' to go up in
the command history,
the prompt is put at the start of the line.
Other places I use vi mode command line editing (e.g., zsh), the
| |
by: ycollet |
last post by:
Hello,
I'm trying to write a program to send python statements to a python
server via tcp and then get back results via a tcp connection. It
nearly works ... but I'm totally lost with the embedded dictionary (I'm
quite new to python).
The first part of the server start the python interpreter via
Py_Initialize() and then waits for python statements.
To send command, I get some strings and evaluate them through
PyRun_String.
|
by: PengYu.UT |
last post by:
Hi,
I feel argparse has some useful things that optparse doesn't have. But
I can't find it argparse in python library reference. I'm wondering
when it will be available in the python standard installation.
Thanks,
Peng
|
by: W. Watson |
last post by:
I have about a 1600 line Pythron program I'd like to make some simple mods
to, but have really just a nodding acquaintance with Python and Tkinter. I
know quite a few languages, including C++. Let's change that. I've not used
anything but C in recent years, and C++ was in my bag before that along with
a number of aged languages.
The biggest change I want to make is the single simple screen titles, top
and bottom. The top was easy, since...
|
by: Cameron Simpson |
last post by:
On 17Aug2008 21:25, John Nagle <nagle@animats.comwrote:
Because $HOSTNAME is a bash specific variable, set by bash but NOT
EXPORTED! Like $0 and a bunch of other "private" variables, subprocesses
do not inherit this value. From "man bash":
Shell Variables
The following variables are set by the shell:
|
by: J-Burns |
last post by:
Hello. Im a bit new to using Tkinter and im not a real pro in
programming itself... :P. Need some help here.
Problem 1:
How do I make something appear on 2 separate windows using Tkinter? By
this I mean that the format would be something like this:
You have Page1 : This has 2-3 buttons on it.
Clicking on each button opens up a new window respectively having
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |