Hi All,
First, let me say that -daniweb- has been a great help for me as I've gotten back into programming over the last 6 months (after a 10 year respite). I've put together some decent code, but Python finally has me stumped. I have some code that generates some very long multi-column data files. I need to sort the data but the rows are too long to import to Excel for sorting. So I want to sort them in Python. I know this is an easy programming task, but I simply can't get the code to sort my data correctly. Here is a sample dataset:
4, 8, 1.3, 0.3, 3, 5537.12, 45.76
4, 8, 1.7, 0.3, 3, 6037.47, 49.9
4, 8, 1.9, 0.3, 2, 5330.66, 44.06
4, 8, 2.1, 0.3, 2, 5330.66, 44.06
4, 9, 0.3, 0.3, 3, 5211.53, 43.07
4, 9, 1.7, 0.3, 3, 5933.43, 49.04
4, 9, 1.7, 0.3, 3, 5933.43, 49.04
4, 9, 1.9, 0.3, 2, 5740.74, 47.44
4, 9, 2.1, 0.3, 2, 5740.74, 47.44
4, 10, 0.3, 0.3, 7, 3950.37, 32.65
4, 10, 0.3, 0.3, 7, 3950.37, 32.65
All I want to do is sort the data by column 5, or any column for that matter. I have written the following code, but it seems to be selecting a character to sort by, rather than a column.
unsorted_data = open('sample_data.txt','r')
data_list = []
for line in unsorted_data.readlines():
data_list.append(line.strip())
data_list.sort(key=lambda x:x[5])
Any help would be greatly appreciated. After two days of reading and writing, I'm going bonkers. g-
ps. I would really love to do a manual "quick sort" because I have a feeling it would be more efficient, but I can't figure that out either. Thx, g-