D
Duncan Booth
Duncan Booth said: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.
1) Python throws an error if you have less than three fields,
requiring more typing to get the same effect.
I would rather have the error when the input isn't formatted as expected.
The alternative would be incorrect output.
If you really want to suppress the error then:
line = (line[:-1]+'\t'*3).split('\t')
2) Python generators on stdin behave strangely. For one thing,
they're not properly line buffered, so you don't get any lines until
eof. But then, eof is handled wrongly, and the loop doesn't exit.
True, if you are trying to reformat interactive input. I had assumed that
the use case here was redirecting input from a file, and in that case the
EOF problem isn't an issue. Buffering may or may not be a problem.
3) There is no efficient RS equivalent, in case you need to read
paragraphs.
In that case I would write a generator to group the lines. Longer than RS,
but also more flexible.