如何按命名列python,csv

时间:2017-11-21 21:27:40

标签: python csv sorting python-2.x

我有三个csv文件,每个文件有三个命名列,'Genus','Species'和'Source'。我将文件合并到一个新文档中,现在我需要按字母顺序排列,首先是属,然后是物种。我想我可以通过首先按字母顺序对物种进行分类,然后是属,然后它们应该按照正确的顺序进行,但是我无法在网上找到任何可以解决如何对命名的字符串列进行排序的内容。我尝试了很多不同的排序方法,但要么没有改变任何东西,要么用最后一个字符串替换第一列中的所有字符串。

这是我合并文件的代码:

import csv, sys

with open('Footit_aphid_list_mod.csv', 'r') as inny:
    reader = csv.DictReader(inny)

    with open('Favret_aphid_list_mod.csv', 'r') as inny:
        reader1 = csv.DictReader(inny)

        with open ('output_al_vonDohlen.csv', 'r') as inny:
            reader2 = csv.DictReader(inny)

            with open('aphid_list_complete.csv', 'w') as outty:
                fieldnames = ['Genus', 'Species', 'Source']
                writer = csv.DictWriter(outty, fieldnames = fieldnames)
                writer.writeheader() 

                for record in reader:
                    writer.writerow(record)
                for record in reader1:
                    writer.writerow(record)
                for record in reader2:
                    writer.writerow(record)

                for record in reader:
                    g = record['Genus']
                    g = sorted(g)
                    writer.writerow(record)

inny.closed
outty.closed

1 个答案:

答案 0 :(得分:2)

如果文件不是非常大,那么将所有行读入单个列表,对其进行排序,然后将其写回:

#!python2
import csv

rows = []

with open('Footit_aphid_list_mod.csv','rb') as inny:
    reader = csv.DictReader(inny)
    rows.extend(reader)

with open('Favret_aphid_list_mod.csv','rb') as inny:
    reader = csv.DictReader(inny)
    rows.extend(reader)

with open('output_al_vonDohlen.csv','rb') as inny:
    reader = csv.DictReader(inny)
    rows.extend(reader)

rows.sort(key=lambda d: (d['Genus'],d['Species']))

with open('aphid_list_complete.csv','wb') as outty:
    fieldnames = ['Genus','Species','Source']
    writer = csv.DictWriter(outty,fieldnames=fieldnames)
    writer.writeheader()
    writer.writerows(rows)