perl to python

Kirk Job-Sluder kirk at eyegor.jobsluder.net
Wed May 12 00:48:54 EDT 2004


On 2004-05-12, Carl Banks <imbosol at aerojockey.invalid> wrote:
> Kirk Job-Sluder wrote:
>> Write me a two-line script in python that reads a character delimited
>> file, and printf pretty-prints all of the records in a different order. 
>
> How about one line (broken into three for clarity):
>
> for line in __import__('sys').stdin:
>     print ''.join([ x.rjust(10) for x in map(
>         line.strip().split(',').__getitem__,[4,3,2,1,0]) ])
>
> Believe it or not, I actually do stuff like this on the command line
> once in awhile; to me, it's less effort to type this in than to
> remember (read: look up) the details of awk syntax.  I don't think I'm
> typical in this regard, though.

This looks like using the proverbial hammer to drive the screw.  

I still find: 
awk 'BEGIN {FS="\t"} {printf("pattern", $1,$4,$3,$2)}' file 

to be more elegant and easier to debug.  It does the required task in
two easy-to remember statements.





More information about the Python-list mailing list