擅长:python、mysql、java
<p>假设所有的<code>filename</code>字段都以一个数字开头,那么最简单的方法就是将整数从文件名中解析出来,按整数排序。你知道吗</p>
<pre><code># Assume this is the data of the CSV after reading it in
filenames = ['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']
# Here, we treat the first part of the filename (the number before the underscore) as the sort key.
sorted_data = sorted(filenames, key=lambda l: (int(l.partition('_')[0])))
</code></pre>
<p>如果您输出<code>sorted_data</code>,它应该如下所示:</p>
<pre><code>['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', '10_somefile,0,0']
</code></pre>