Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   where is the awk to python translator program (http://www.velocityreviews.com/forums/t320141-where-is-the-awk-to-python-translator-program.html)

Dan Jacobson 07-26-2003 03:26 AM

where is the awk to python translator program
 
An old dog can't learn new tricks, so where's the a2py awk to python
translator? Perl has a2p. E.g. today I wonder how to do '{print
$1}', well with a2p I know how to do it in perl, but with python I am
supposed to hunker down with the manuals.

Skip Montanaro 07-26-2003 06:25 AM

Re: where is the awk to python translator program
 

Dan> An old dog can't learn new tricks, so where's the a2py awk to python
Dan> translator? Perl has a2p.

Sorry, Python doesn't have such a beast. There's never been enough demand.

Dan> E.g. today I wonder how to do '{print $1}', well with a2p I know
Dan> how to do it in perl, but with python I am supposed to hunker down
Dan> with the manuals.

Python doesn't have the implicit looping and splitting into fields that awk
has, so you need to loop over the input and split the fields yourself:

import sys
for line in sys.stdin:
print line.split()[0] # == '{print $1}'

There's also no global field separator variable. Suppose I wanted to print
the first field in each row of my passwd file. The split() method, when
called as above with no arguments, will split the string on any runs of
whitespace. If given a string parameter it will split the string on that
string. Here's some input and output pasted from an interactive session:

>>> for line in file("/etc/passwd"):

... print line.split(":")[0]
...
root
bin
daemon
adm
lp
... and so on ...

Obviously, Python is not as concise as awk for the sorts of things awk is
good at, but then Python is good for a lot more things than awk is. ;-)

Skip


bthoren 07-28-2003 05:09 PM

Re: where is the awk to python translator program
 
Why translate? PyAWK may do what you need:

http://pyawk.sourceforge.net/

I don't have any direct experiences with it, but it does look
interesting.


Dan Jacobson wrote:

> An old dog can't learn new tricks, so where's the a2py awk to python
> translator? Perl has a2p. E.g. today I wonder how to do '{print
> $1}', well with a2p I know how to do it in perl, but with python I am
> supposed to hunker down with the manuals.




All times are GMT. The time now is 12:22 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.