views:

158

answers:

2

I have a text file with lines like this:

2010-02-18 11:46:46.1287 bla
2010-02-18 11:46:46.1333 foo
2010-02-18 11:46:46.1333 bar
2010-02-18 11:46:46.1467 bla

A simple sort would swap lines 2 and 3 (bar comes before foo), but I would like to keep lines (that have the same date/time) in their original order.

How can I do this in Python?

Bonus question: Can GNU sort also do this?

+20  A: 
sorted(array, key=lambda x:x[:24])

Example:

>>> a = ["wxyz", "abce", "abcd", "bcde"]
>>> sorted(a)
['abcd', 'abce', 'bcde', 'wxyz']
>>> sorted(a, key=lambda x:x[:3])
['abce', 'abcd', 'bcde', 'wxyz']
KennyTM
Reference: http://docs.python.org/library/functions.html#sorted
S.Lott
Note that this sorts the the date/time as a string. That happens to work in this case. Otherwise you might need to use a smarter key function which actually parses the date/time.
Jon-Eric
+4  A: 

The built-in sort is stable, so you the effectively-equal values stay in order by default.

import operator

with open('filename', 'r') as f:
    sorted_lines = sorted(f, key=operator.itemgetter(slice(0, 24)))

At this point sorted_lines will be a list of the sorted lines. To replace the old file, make a new file, call new_file.writelines(sorted_lines), then move the new file over the old one.

Mike Graham
+1 for explaining that it works because `sorted` is stable
fortran
`s/slice(24, None)/slice(0, 24)/`
J.F. Sebastian
@J.F. Sebastian, Thanks, I misread the question (and thought it conflicted with the title). Also, your regex is wrong. ;)
Mike Graham