Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > where is the awk to python translator program

Reply
Thread Tools

where is the awk to python translator program

 
 
Dan Jacobson
Guest
Posts: n/a
 
      07-26-2003
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.
 
Reply With Quote
 
 
 
 
Skip Montanaro
Guest
Posts: n/a
 
      07-26-2003

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

 
Reply With Quote
 
 
 
 
bthoren
Guest
Posts: n/a
 
      07-28-2003
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.


 
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
ruby -> python translator exists? rogerdpack Python 3 12-27-2008 05:49 AM
re: bronco50 postet -> Power Translator Pro v8 0 Multilang- "Power Translator Pro v8.0 Multilang.sfv" (0/1) spiman@gmx.ch Computer Support 0 06-12-2005 07:51 PM
Re: Python interpreter in Basic or a Python-2-Basic translator. rrr@ronadam.com Python 0 05-02-2005 01:48 PM
Python interpreter in Basic or a Python-2-Basic translator. Engineer Python 6 05-01-2005 10:16 PM
PHP to Python Translator? Bruce Dickey Python 0 09-27-2003 03:45 PM



Advertisments