I'm trying to sort a csv file that looks like this:
filename,field1,field2
10_somefile,0,0
1_somefile,0,0
2_somefile,0,0
3_somefile,0,0
4_somefile,0,0
5_somefile,0,0
6_somefile,0,0
7_somefile,0,0
8_somefile,0,0
9_somefile,0,0
I've referenced code from another thread:
with open(outfname, "rb") as unsorted_file:
csv_f = csv.reader(unsorted_file)
header = next(csv_f, None)
sorted_data = sorted(csv_f, key=operator.itemgetter(0))
with open(outfname, 'wb') as sorted_file:
csv_f = csv.writer(sorted_file, quoting=csv.QUOTE_ALL)
if header:
csv_f.writerow(header)
csv_f.writerows(sorted_data)
However, this won't move the '10_somefile' to the end. How can I sort this such that it uses the number before the underscore as the sorting field?