擅长:python、mysql、java
<p>读取整个文件,排序并写入新文件:</p>
<pre><code>with open('file1', 'r') as f:
lst = f.readlines()
with open('file2', 'w') as w:
w.writelines(sorted(lst))
</code></pre>
<p>如果“排序”应该使用一些特殊的排序,请在“key”参数中提供比较函数:</p>
<pre><code>print(help(sorted))
</code></pre>
<blockquote>
<p>sorted(iterable, /, *, key=None, reverse=False)<br/>
Return a new list containing all items from the iterable in ascending order.</p>
<p>A custom key function can be supplied to customize the sort order, and the
reverse flag can be set to request the result in descending order.</p>
</blockquote>