简体   繁体   中英

How to sort a file alphabetically by named column, python, csv

I have three csv files each with three named columns, 'Genus', 'Species', and 'Source'. I merged the files into a new document and now I need to alphabetize the columns, first by genus and then by species. I figured I could do this by first alphabetizing the species, and then the genus and then they should be in the proper order, but I haven't been able to find anything online that addresses how to sort named columns of strings. I tried lots of different ways of sorting, but it either didn't change anything or replaced all the string in the first column with the last string.

Here's my code for merging the files:

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

If you files aren't insanely large, then read all the rows into a single list, sort it, then write it back:

#!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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM