crimes in Python

Remco Gerlich scarblac-spamtrap at pino.selwerd.nl
Wed Mar 8 17:30:15 CET 2000


Kragen Sitaker wrote in comp.lang.python:
> >Otherwise, I suppose:
> >while 1:
> >   line = sys.stdin.readline()
> >   if not line:
> >      break
> 
> I thought about that.  I decided it was just as icky, especially given
> that I couldn't outdent the if not line the way I would in other
> languages.  (So my loop control would be hard to find.)

It's a very common idiom in Python. I try to keep the breaks near the 
while 1:. That seems to be enough.

> The string % operator seems to be a source of great sweetness and
> light.  I should use it more.

There's a cool trick with that operator as well. Say, you have a
dictionary like
>> x = {'CRIME': '2911.02', 'TYPE': 'ROBBERY - FORCE, THR'}

Now observe this:
>>> print "Crime: %(CRIME)s\tType: %(TYPE)s" % x
Crime: 2911.02  Type: ROBBERY - FORCE, THR

That'll help with output formatting since you basically have a
list of dicts :)

> >My intuition is that there should be a clean way without regexpen. But I
> >can't think of any right now. If this works, keep it :-)
> 
> Well, you really do need a state machine with several states; regexps
> are a compact and (theoretically) understandable way to write those.

True. I must say, I've *never* need them in Python yet, so I couldn't
actually see what was happening exactly :)

> Actually, it is \r\n --- the file was written by Microsoft Excel, and
> Python is running on Linux.

Ah. Didn't think of that.


-- 
Remco Gerlich,  scarblac at pino.selwerd.nl
Hi! I'm a .sig virus! Join the fun and copy me into yours!



More information about the Python-list mailing list