Daniel@0: # Part of DML (Digital Music Laboratory) Daniel@0: # Copyright 2014-2015 Samer Abdallah, University College London Daniel@0: Daniel@0: # This program is free software; you can redistribute it and/or Daniel@0: # modify it under the terms of the GNU General Public License Daniel@0: # as published by the Free Software Foundation; either version 2 Daniel@0: # of the License, or (at your option) any later version. Daniel@0: # Daniel@0: # This program is distributed in the hope that it will be useful, Daniel@0: # but WITHOUT ANY WARRANTY; without even the implied warranty of Daniel@0: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Daniel@0: # GNU General Public License for more details. Daniel@0: # Daniel@0: # You should have received a copy of the GNU General Public Daniel@0: # License along with this library; if not, write to the Free Software Daniel@0: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Daniel@0: Daniel@0: # -*- coding: utf-8 -*- Daniel@0: __author__="samer" Daniel@0: Daniel@0: import csv Daniel@0: from warnings import warn Daniel@0: Daniel@0: import sys Daniel@0: import time Daniel@0: def print_status(msg): sys.stderr.write(msg+'\n') Daniel@0: Daniel@0: # call function for each row of a CSV that has ncols columns. Daniel@0: # A warning is printed if any rows have the wrong number of columns. Daniel@0: # Daniel@0: # csv_for_each : string, integer, (list(string) -> void) -> void Daniel@0: def csv_for_each(filename,ncols,f): Daniel@0: badcount=0 Daniel@0: with open(filename,'rb') as csvfile: Daniel@0: contents=csv.reader(csvfile, delimiter=',',quotechar='"') Daniel@0: for row in contents: Daniel@0: if len(row)==ncols: f(row) Daniel@0: else: badcount += 1 Daniel@0: # if badcount>0: warn("Ignoring %d malformed rows in CSV file %s" % (badcount,filename)) Daniel@0: Daniel@0: # map f over list of rows from CSV to list of values Daniel@0: # Daniel@0: # csv_map_rows : string, integer, (list(string) -> A) -> list(A) Daniel@0: def csv_map_rows(filename,ncols,f): Daniel@0: good=[] Daniel@0: def append_row(row): good.append(f(row)) Daniel@0: csv_for_each(filename,ncols,append_row) Daniel@0: return good Daniel@0: Daniel@0: def id(x): return x Daniel@0: Daniel@0: Daniel@0: # returns selected columns of CSV, one list per column Daniel@0: # Daniel@0: # csv_columns : string, integer, list(integer) -> list(list(string)) Daniel@0: def csv_columns(filename,ncols,columns): Daniel@0: def getter(i): return lambda r:r[i] Daniel@0: return csv_map_columns(filename,ncols,map(getter,columns)) Daniel@0: Daniel@0: # returns selected columns of CSV, one list per column, with Daniel@0: # a function supplied for converting one row of strings to a row of data Daniel@0: # Daniel@0: # csv_map_columns : string, integer, list(list(string)->A) -> list(list(A)) Daniel@0: def csv_map_columns(filename,ncols,sels): Daniel@0: ii=range(0,len(sels)) Daniel@0: data=[[] for i in ii] Daniel@0: def append_columns(row): Daniel@0: for i in ii: data[i].append(sels[i](row)) Daniel@0: csv_for_each(filename,ncols,append_columns) Daniel@0: Daniel@0: return data