String processing in Python

I’m trying to teach myself Python, and I’ve got a reasonably simple task set out for myself. Basically, I want to read in a comma-delimited file, get the individual fields, and do some stuff with them. In Perl, what I’m trying to do is fairly simple:


if ($s =~ m/(\S+)\,(\S+)\,(\S+)\,(\S+)/)
{
    $a = $2;
    $b = $3;
    $c = $4;
}

What’s the equivalent Python code to do this?

Oy vey. First of all, you’d do much better using split in Perl for that task. Python also has a split method which you can call on any string object. To split a string foo on commas:

foo.split(‘,’)

split() works here, but suppose that I was trying to do something a bit more complicated:


if ($s =~ m/\((\d+)\) (\d+)-(\d+)/)
{
    $s = $2 . $3 . $4;
}

Is there a comparably succinct way to do that in Python, or will I have to do three splits and remove some punctuation explicitly?

Ignore the fact that I’m not validating the number of digits; that just really distracts from the basic question.

Python has pretty good support for regexes (though they don’t have every feature that Perl’s do.) To use one, you create and compile a regex object, then call its ‘match’ method against a string. You can then extract the capture groups from the resulting match object.



import re

pat = re.compile( '\((\d+)\) (\d+)-(\d+)' )

capt = pat.match( '(123) 555-1234' )

print capt.group( 1, 2, 3 )


(Note: the above is untested as I don’t have Python installed here at work. :frowning: Hopefully it’s enough to get you started. There’s a pretty good tutorial on Python regexes here. )

Beaten to it, and friedo’s code works. Note that re has only been standard since Python 1.5.

Thanks guys.