Count the tabs; if you presume that the note field never has 4 tabs on one line in it, you can collect the note until you find a line that does have 4 tabs in it:
def collapse_newlines(s):
# Collapse multiple consecutive newlines into one; removes trailing newlines
return '\n'.join(filter(None, s.split('\n')))
def read_tabbed_file(filename):
with open(filename) as f:
row = None
for line in f:
if line.count('\t') < 4: # Note continuation
row[-1] += line
continue
if row is not None:
row[-1] = collapse_newlines(row[-1])
yield row
row = line.split('\t')
if row is not None:
row[-1] = collapse_newlines(row[-1])
yield row
The above generator function will not yield a row until it is certain that there is no note continuing on the next line, effectively looking ahead.
Now use the read_tabbed_file()
function as a generator and loop over the results:
for row in read_tabbed_file(yourfilename):
# row is a list of elements
Demo:
>>> open('/tmp/test.csv', 'w').write('User Name\tCode\tTrack\tColor\tNote\n\nUser Name2\tCode2\tTrack2\tColor2\tNote2\n')
>>> for row in read_tabbed_file('/tmp/test.csv'):
... print row
...
['User Name', 'Code', 'Track', 'Color', 'Note']
['User Name2', 'Code2', 'Track2', 'Color2', 'Note2']