在磁盘上而不是内存中对大型csv文件进行排序
typedcsvsort的Python项目详细描述
CSV排序
用于排序磁盘上不适合内存的csv文件。合并排序算法用于将原始文件分解为较小的块,在内存中对这些块进行排序,然后合并这些排序的文件。
示例用法
>>>fromcsvsortimportcsvsort>>># sort this CSV on the 5th(str) and 3rd(int) columns (columns are 0 indexed)>>>csvsort('test1.csv',[4,2],["str","int"])>>># sort this CSV with no header on 4th column and save results to separate file>>>csvsort('test2.csv',[3],["str"],output_file='test3.csv',has_header=False)>>># sort this TSV on the first column and use a maximum of 10MB per split>>>csvsort('test3.tsv',[0],["str"],max_size=10,delimiter='\t')>>># sort this CSV on the first column and force quotes around every field (default is csv.QUOTE_MINIMAL)>>>importcsv>>>csvsort('test4.csv',[0],["str"],quoting=csv.QUOTE_ALL)
安装
支持python 2&3:
$ pip install csvsort $ pip3 install csvsort