View Full Version : String processing in Python
02-14-2008, 03:11 PM
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?
02-14-2008, 03:17 PM
Oy vey. First of all, you'd do much better using split (http://perldoc.perl.org/functions/split.html) 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:
02-14-2008, 05:36 PM
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.
02-14-2008, 05:59 PM
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.
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. :( Hopefully it's enough to get you started. There's a pretty good tutorial on Python regexes here (http://www.amk.ca/python/howto/regex/regex.html). )
02-14-2008, 06:03 PM
Beaten to it, and friedo's code works. Note that re has only been standard since Python 1.5.
02-14-2008, 06:45 PM
vBulletin® v3.7.3, Copyright ©2000-2013, Jelsoft Enterprises Ltd.