1

I am reading in a series of .csv files which are in the form of:

ImageData_999.csv
ImageData_1000.csv

As you can see the number increase sequentially but they do not have leading zeros and so when they get read in I am currently looking at a list that ends like this:

ImageData_3259
ImageData_3289
ImageData_811
ImageData_907

The files do skip around in number so there won't be 3289 files if that is the largest file name but I want to sort them so that they are properly ordered. That is what is below instead of what is above:

ImageData_811
ImageData_907
ImageData_3259
ImageData_3289

I am working in python and currently just getting the file names from the os module like so:

for root, dirs, files in os.walk(path):
    for file in fnmatch.filter(files, filter):
        yield os.path.join(root, file)

I have tried a few methods to sort the list that this produces but none of them seem to change the order.

badrobit
  • 743
  • 1
  • 11
  • 25
  • 3
    This is called a "natural sort" and of course it's been asked before. Here's one: http://stackoverflow.com/questions/4836710/does-python-have-a-built-in-function-for-string-natural-sort – Mark Ransom Mar 04 '15 at 20:26
  • And here's the one I was remembering: http://stackoverflow.com/questions/12184015/in-python-how-can-i-naturally-sort-a-list-of-alphanumeric-strings-such-that-alp – Mark Ransom Mar 04 '15 at 20:29
  • That worked if you could post it as an answer I could mark this as solved :) – badrobit Mar 04 '15 at 20:37
  • The usual way is to get it closed as a duplicate. I'll give it my vote right now, it will probably be closed right away. – Mark Ransom Mar 04 '15 at 20:39
  • I was in the middle of adding this answer, and the question got marked as duplicate. I think this is simpler: `sorted(filenames, key=lambda x: int(x.split('_')[1]))` – 1.618 Mar 04 '15 at 20:47

2 Answers2

2

As Mark Ransom posted here is the answer that worked for me:

Does Python have a built in function for string natural sort?

Community
  • 1
  • 1
badrobit
  • 743
  • 1
  • 11
  • 25
0

So you want to sort them numerically not lexicographically?

Does the filename always start with the same word? "ImageData_"?

If so I would just strip out the letters and then sort them as numbers. (E.g. for each of them filename = re.sub(r'[^0-9]*', "", filename)).

And then you can sort them as numbers like this list1.sort(key=int) from How to sort a list numerically?

Community
  • 1
  • 1
Beth Crane
  • 625
  • 3
  • 8