googleboy wrote: > > I am reading in a csv file that documents a bunch of different info on > about 200 books, such as title, author, publisher, isbn, date and > several other bits of info too. > ... > I really want to be able to sort the list of books based on other > criterium, and even multiple criteria (such as by author, and then by > date.)
import string input = open(r'c:\books.csv', 'r') records = input.readlines() input.close() # assuming first line contains headers headers = records.pop(0) records = [x.strip().split(',') for x in records] # header order p_headers ='(title, author, publisher, isbn, date, other)' p_sorts = '(author, title, date, publisher, other, isbn)' temp_records = [] for r in records: exec '%(p_headers)s = r' % vars() exec 't = %(p_sorts)s' % vars() temp_records.append(t) temp_records.sort() sorted_records = [] for t in temp_records: exec '%(p_sorts)s = t' % vars() exec 'r = %(p_headers)s' % vars() sorted_records.append(r) lines = [headers] + [','.join(x)+'\n' for x in sorted_records] output = open(r'c:\output.csv', 'w') output.writelines(lines) output.close() -- http://mail.python.org/mailman/listinfo/python-list