[Python Wpg] More on hating brackets, commas, and quotes
Stuart Williams
stuartw at mts.net
Fri Apr 28 16:13:20 EDT 2006
Bill and others,
I mentioned at Tuesday's meeting how to use a regular expression to
split text on indentations to stuff into a nested list. See below for
how that works - very similar to the code we saw at the meeting.
Hmmm... I wonder how many people will stumble upon this message doing
web searches for the names of the hockey players in the sample data.
Stuart.
import pprint
import re
fan_data = """
Charles
Daniel Alfredsson
Pavel Datsyuk
Mike Modano
Steve Bernier
Jeff
Joe Thornton
Eric Staal
Adam
Jonathan Cheechoo
Jere Lehtinen
Michael Ryder
Patrick Marleau
"""
fans = []
# strip leading and trailing newline(s) and split on un-indented entries
for para in re.split(r'\n\b', fan_data.strip()):
lines = para.split('\n')
# create tuple of first line followed by list of stripped remaining lines
fans.append((lines[0],
[player.strip() for player in lines[1:]]))
pprint.pprint(fans)
More information about the Winnipeg
mailing list