This is sort of two coding style questions in one. I have a tab-dl’d file where ‘objects’ are separated by a blank line. The first line of the ‘object’ is the ID. The remaining lines up until a blank line are things that belong to the object. I want to parse this into a hash as below:
f = open(someFile, 'rb')
c = csv.reader(f, delimiter = "\t", quoting = csv.QUOTE_NONE)
thingstore = {}
try:
for row in c:
title = row[0]
thingstore[title] = set()
item = map(fixStupidExcelCrap, c.next())
while ''.join(item).strip() != '':
thingstore[title].add(tuple(item))
item = map(fixStupidExcelCrap, c.next())
except StopIteration:
pass
f.close()
There are a couple things I think are ugly about this solution. Firstly, having the try block surrounding the entire function seems like asking for problems as improperly formatted files may not be detected. One alternative is wrapping each next() call in a try block and setting a flag to exit the outer loop which also seems hacky.
Secondly, while ''.join(item).strip() != '': is very ugly. Is there a better way to test for an empty line that’s been parsed by the csv module?
Update:
I missed a detail that affects the test for an empty line. As you might have guessed, the code is parsing a tab delimited file exported from Excel. The fun thing about empty lines in this scenario is that they’re not really empty – all lines in the file have the same number of tabs. So if you have 3 columns in the excel file, an empty line in the exported tab delimited file will have 2 tabs in it and csv will parse that into ['', '', ''] which bool evaluates to True.
So wrt Ignacio’s much prettier answer, for row in itertools.takewhile(bool, c): won’t work because it’ll snarf up the rest of the file, empty lines and ID rows included. for row in itertools.takewhile(lambda x: ''.join(x).strip() != '', c): does work, but we’re back to the ugliness I was trying to avoid (the strip() probably isn’t necessary, but I put it in to be on the safe side).
Blech. Empty lines result in an empty list.