views:

57

answers:

3

I want to turn something like this

CS 240, CS 246, ECE 222, ... (more or less); Software Engineering students only

into

('CS 240', 'CS 246', 'ECE 222', 'ECE 220')

in Python, code that matches a single course looks like

>>> re.search('([A-Z]{2,5} \d{3})', 'SE 112').groups()
('SE 112',)

I prefer a regular expression only method because I have a bunch of other alternate reg exps using '|' to combine them. However, a method with split is acceptable.

+5  A: 
>>> a="CS 240, CS 246, ECE 222"
>>> b=tuple(a.strip() for a in a.split(','))
>>> b
('CS 240', 'CS 246', 'ECE 222')
>>> 
voyager
A: 

This method uses regular expressions and matches your input:

>>> import re
>>> re.findall("\w+\s\d+", "CS 240, CS 246, ECE 222")
['CS 240', 'CS 246', 'ECE 222']

It does not look for the comma. Instead it looks for anything but the comma: it first matches multiple word characters, then a space character, then multiple digits. Findall looks for all occurrences of this pattern.

Frederik
+3  A: 

Isn't the csv standard library module ( http://docs.python.org/library/csv.html ) what you are looking for?

Jacek Konieczny