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,d,d,d,d,d,d,d,d,d,d,d,d,d
+d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d
+d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d

6925551/parser.py

+import itertools
+import operator
+import re
+import csv
+import pprint
+
+class TaggedLine(str):
+	"""
+	Override str to allow a tag to be added.
+	"""
+	def __new__(cls, val, tag):
+		return str.__new__(cls, val)
+
+	def __init__(self, val, tag):
+		super(TaggedLine, self).__init__(val)
+		self.tag = tag
+
+def sections(stream):
+	"""
+	Tag each line of the stream with its [section] (or None)
+	"""
+	section_pattern = re.compile('\[(.*)\]')
+	section = None
+	for line in stream:
+		matcher = section_pattern.match(line)
+		if matcher:
+			section = matcher.group(1)
+			continue
+		yield TaggedLine(line, section)
+
+def splitter(stream):
+	"""
+	Group each stream into sections
+	"""
+	return itertools.groupby(sections(stream), operator.attrgetter('tag'))
+
+def parsed_sections(stream):
+	for section, lines in splitter(stream):
+		yield section, list(csv.reader(lines))
+
+if __name__ == '__main__':
+	with open('data.csv') as stream:
+		for section, data in parsed_sections(stream):
+			print 'section', section
+			pprint.pprint(data[:2])
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.