回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我正在尝试对一个csv文件进行排序,该文件如下所示:</p>
<pre><code>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
</code></pre>
<p>我引用了另一个线程的代码:</p>
<pre><code>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)
</code></pre>
<p>但是,这不会将“10\u somefile”移到末尾。如何排序,使其使用下划线前的数字作为排序字段?你知道吗</p>