I am brand new to python and don’t really know too much. I am trying to sort a file with thousands of lines by a specific field. Let’s say my file info.txt I want to sort looks something like:
test-23 2012 05 34 object-1-2
test-21 2012 04 23 object-4-5
test-09 2011 12 55 object-1-2
test-11 2010 12 72 object-3-4
test-12 2010 17 22 object-1-2
I’m trying to sort the file so that all the object-x-x are grouped together and output to say another file. Like so:
test-12, 2010, 17, 22, object-1-2
test-23, 2012, 05, 34, object-1-2,
test-09 2011, 12, 55, object-1-2
test-11 2010, 12, 72, object-3-4
test-21 2012, 04, 23, object-4-5
I’m trying to make a class out of this data, and what I’ve done so far is split the fields using the .split(” ,”) I get something like this for the first line:
'test-12', '2010', '17', '22', 'object-1-2'
and so on for all the other lines…
Now that it is split, how can I make a class out of this? Such that I can assign each field?
If the lines are in the list
lines, you can sort them thusly:This will sort them by field 4, the “object-x-y” field.