peru_45
peru_45

Reputation: 310

Sort elements in a list based on a csv column (Python)

I have a list that contains different paths to files. I want to sort the elements in this list by matching the stem of these paths (i.e. the file name) with a column in a csv file that contains the file names. This is to make sure that the list displays elements in order of the file names that are contained in the csv. The csv is similar to as shown below:

enter image description here

I have done the following:

file_list = ['C:\\Example\\SS\\e342-SFA.jpg', 'C:\\Example\\DF\\j541-DFS.jpg', 'C:\\Example\\SD\\p162-YSA.jpg']

for f in file_list:
    x = Path(f).stem # grabs file name from file_list without .jpg
    for line in csv_file:
        IL = line.replace(":", "").replace("\n", "").replace("(", "").replace(")", "")
        columns = IL.split(",")
        if columns[3] == x: # column[3] = File name in csv
            [do the sorting]

I'm not sure how to proceed further from here.

Upvotes: 1

Views: 108

Answers (1)

Stef
Stef

Reputation: 15525

I'll assume you already know how to open and parse a csv file, and hence you already have the list ['p162-YSA', 'e342-SFA', 'j541-DFS'].

from ntpath import basename, splitext
 
order_list = ['p162-YSA', 'e342-SFA', 'j541-DFS']
file_list = ['C:\\Example\\SS\\e342-SFA.jpg', 'C:\\Example\\DF\\j541-DFS.jpg', 'C:\\Example\\SD\\p162-YSA.jpg']
 
order_dict = {}
for i, w in enumerate(order_list):
    order_dict[w] = i
# {'p162-YSA': 0, 'e342-SFA': 1, 'j541-DFS': 2}
 
sorted_file_list = [None] * len(file_list)
for name in file_list:
    sorted_file_list[ order_dict[splitext(basename(name))[0]] ] = name
 
print(sorted_file_list)
# ['C:\\Example\\SD\\p162-YSA.jpg', 'C:\\Example\\SS\\e342-SFA.jpg', 'C:\\Example\\DF\\j541-DFS.jpg']

Note: I chose to import basename and splitext from ntpath rather than from os.path so that this code can run on my linux machine. See this related question: Get basename of a Windows path in Linux.

Upvotes: 1

Related Questions