perl to python

Duncan Booth me at privacy.net
Wed May 12 08:30:27 EDT 2004


Kirk Job-Sluder <kirk at eyegor.jobsluder.net> wrote in 
news:slrnca3t0e.2asc.kirk at eyegor.jobsluder.net:

> I'll be more specific about the challenge.  Using only stock python with
> no added modules, give me a script that pretty-prints a
> character-delimted file using one variable assignment, and one function.
> 
> Here is the solution in awk:
> BEGIN { FS="\t" } 
> {printf("%s %s %s %s", $4, $3, $2, $1)}
> 
> 
> 

One assignment statement and one function call is easy. Of course, you 
could argue that more than one name gets rebound, but then that is also 
true of the awk program:

import sys
for line in sys.stdin:
    line = line[:-1].split('\t')
    print "%s %s %s %s" % (line[3], line[2], line[1], line[0])

While I agree with you that using the appropriate tool is preferred over 
using Python for everything, I don't really see much to choose between the 
Python and awk versions here.



More information about the Python-list mailing list