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-

The problem is that your line is a character string, so line[5] is the fifth character. You must split the lines to obtain arrays of 7 items, and get the fifth item like this

data_list.sort(key= lambda line: float(line.split(",")[5]))

also you don't nedd to write a loop to get the data

data_list = [line.strip() for line in open("sample_data.txt")]

produces the same data ;)

Since I am using Python 2.3, I will define a function to pass to sort().

def comp(a,b):
    return cmp(a[5], b[5])

f = open('sample_data.txt')
data_list = [[float(item) for item in line.strip().split(',')] for line in f]
data_list.sort(comp)
f.close()

When you read the information from file, each line is a string as in:

"4, 8, 1.3, 0.3, 3, 5537.12, 45.76"

In your code you are creating a file object, then iterating on obj.readlines().

Well, no doubt about it, you guys have made my day! Thanks SO much. grant-

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.