builtin regular expressions?
Duncan Booth
duncan.booth at invalid.invalid
Sat Sep 30 11:19:12 EDT 2006
Jorge Godoy <jgodoy at gmail.com> wrote:
> See if this isn't better to read:
>
>========================================================================
>========
> def print_message(some_str):
> if some_str.startswith('track='):
> print "Your track is", some_str[6:]
> elif some_str.startswith('title='):
> print "It's a title of", some_str[6:]
> elif some_str.startswith('artist='):
> print "It was played by", some_str[7:]
> else:
> print "Oops, I dunno the pattern for this line..."
> return
>
> for line in ['track="My favorite track"', 'title="My favorite song"',
> 'artist="Those Dudes"', 'Something else']:
> print_message(line)
>========================================================================
>========
>
> Expected output:
Or you could make it even clearer by using a loop:
messages = [
('track=', 'Your track is'),
('title=', "It's a title of"),
('artist=', "It was played by"),
]
def print_message(s):
for key, msg in messages:
if s.startswith(key):
print msg,s[len(key):]
break
else:
print "Oops, I dunno the pattern for this line..."
for line in ['track="My favorite track"', 'title="My favorite song"',
'artist="Those Dudes"', 'Something else']:
print_message(line)
More information about the Python-list
mailing list