Previous Entry Share Next Entry

Python question: How do do something like perl's <>

In perl you can use <> to mean "read one line at a time from each file in ARGV or just stdin if no files are listed". It is wonderful because in 2 keystrokes you get functionality that is similar to how Unix tools like cat, grep, and many others work.

Is there a similar thing in Python? I can't find it.


  • 1
You want the fileinput module. Admittedly,
import fileinput
for line in fileinput.input():
    process line

is more than two keystrokes.

Thank you!

Damn, I should write a "python guide for perl people" article. There are 6-7 idioms from perl that don't translate obviously and "Learning Python" doesn't cover them.

  • <>
  • simple while loop with a next for comments and a m//;
  • simple while loop with a s///;
  • argv processing for something simple (when FLAGS is overkill)
  • initializing a hash (covered in Learning Python)
  • defined() (or at least "read an element and default to foo if it doesn't exist")
  • perl -MO=Deparse

>>> ff = open("/etc/passwd")
>>> for line in ff:
... print ">>", line

>>> import re
>>> ff = open("/etc/passwd")
>>> for line in ff:
... if re.match("daemon:", line):
... print line

>>> re.sub("el", "ul", "hello")

import getopt
# but like C's getopt, that's not so simple that it'd make a short example.

>>> info = {"parrot": "dead", "year": 1969}
>> print info.get("whale", "buzzard")

No idea about the last one.

Yes, I know all those. i was just listing them as idioms that perl programmers would want to know (and generally aren't in the first 2/3rds of the Learning Python book)

The klunkiness of the re syntax especially pisses me off. I remember that when I was learning Python I kept looking for simple examples that would be similar to what I had done in Perl but kept finding these complicated examples with re.compile() and little mention of the simple re.match() function.

One of my pet peeves in man pages and other technical docs is when they are focused on showing off the complicated 'cool' cases instead of starting with basics and working their way up. There are a lot of man pages that that and it pisses me off. For example, find's explanation of "-prune" doesn't include the most common use case, join/paste are so caught up with bragging about what complicated things they can handle that they don't show what the commands are actually used for. (this may have been fixed recently... I'm not logging in to check :) )

I got into the habit of using re objects in Python; I think it actually reads more easily that way in some cases. The module docs could certainly be clearer, though.

man pages are references, not tutorials. One of my pet peeves is overly-long man pages that are so full of "examples" that you can't use them as a reference.

There are good reasons to use re.compile instead of re.match in nontrivial programs (e.g. library functions): the regexp cache in Python has a finite size.

As for -prune, stop whining and send me a patch already :)

IMO you're wrong about join/paste, though: the reason the manpages are so useless is that they are automatically generated from the --help output.

Do the easier one first: "Perl guide for ksh/bash programmers".

The nearest thing would be "The Camel Book". At least it was that way when I read it. Though, that was the first edition.

Ruby with the each method on File I/O (or even Open)

  • 1

Log in

No account? Create an account