I am trying to sort a file by a quick sort comparison. The problem lies in parsing the string and comparing fields. I use the following to break up a line:
records = [line.split(spl) for line in file(filename)]
and the following to sort:
records.sort( lambda a,b: cmp(a[fieldNo],b[fieldNo]) )
This works fine on files delimited with "x", "y", "z". If I have X, Y, Z or X\tY\tZ, it blows up and cannot parse the line. I have tried defining 'spl' as comma (','), tab (\t) and empty (' ') and none of them work. It only runs properly if it's quote/comma delimited. Is there a library (besides CSV) that will work with a variety of delimiters? or a workaround? CSV does not have the split method.