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