Daniel@0
|
1 # Part of DML (Digital Music Laboratory)
|
Daniel@0
|
2 # Copyright 2014-2015 Samer Abdallah, University College London
|
Daniel@0
|
3
|
Daniel@0
|
4 # This program is free software; you can redistribute it and/or
|
Daniel@0
|
5 # modify it under the terms of the GNU General Public License
|
Daniel@0
|
6 # as published by the Free Software Foundation; either version 2
|
Daniel@0
|
7 # of the License, or (at your option) any later version.
|
Daniel@0
|
8 #
|
Daniel@0
|
9 # This program is distributed in the hope that it will be useful,
|
Daniel@0
|
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Daniel@0
|
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Daniel@0
|
12 # GNU General Public License for more details.
|
Daniel@0
|
13 #
|
Daniel@0
|
14 # You should have received a copy of the GNU General Public
|
Daniel@0
|
15 # License along with this library; if not, write to the Free Software
|
Daniel@0
|
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
Daniel@0
|
17
|
Daniel@0
|
18 # -*- coding: utf-8 -*-
|
Daniel@0
|
19 __author__="samer"
|
Daniel@0
|
20
|
Daniel@0
|
21 import csv
|
Daniel@0
|
22 from warnings import warn
|
Daniel@0
|
23
|
Daniel@0
|
24 import sys
|
Daniel@0
|
25 import time
|
Daniel@0
|
26 def print_status(msg): sys.stderr.write(msg+'\n')
|
Daniel@0
|
27
|
Daniel@0
|
28 # call function for each row of a CSV that has ncols columns.
|
Daniel@0
|
29 # A warning is printed if any rows have the wrong number of columns.
|
Daniel@0
|
30 #
|
Daniel@0
|
31 # csv_for_each : string, integer, (list(string) -> void) -> void
|
Daniel@0
|
32 def csv_for_each(filename,ncols,f):
|
Daniel@0
|
33 badcount=0
|
Daniel@0
|
34 with open(filename,'rb') as csvfile:
|
Daniel@0
|
35 contents=csv.reader(csvfile, delimiter=',',quotechar='"')
|
Daniel@0
|
36 for row in contents:
|
Daniel@0
|
37 if len(row)==ncols: f(row)
|
Daniel@0
|
38 else: badcount += 1
|
Daniel@0
|
39 # if badcount>0: warn("Ignoring %d malformed rows in CSV file %s" % (badcount,filename))
|
Daniel@0
|
40
|
Daniel@0
|
41 # map f over list of rows from CSV to list of values
|
Daniel@0
|
42 #
|
Daniel@0
|
43 # csv_map_rows : string, integer, (list(string) -> A) -> list(A)
|
Daniel@0
|
44 def csv_map_rows(filename,ncols,f):
|
Daniel@0
|
45 good=[]
|
Daniel@0
|
46 def append_row(row): good.append(f(row))
|
Daniel@0
|
47 csv_for_each(filename,ncols,append_row)
|
Daniel@0
|
48 return good
|
Daniel@0
|
49
|
Daniel@0
|
50 def id(x): return x
|
Daniel@0
|
51
|
Daniel@0
|
52
|
Daniel@0
|
53 # returns selected columns of CSV, one list per column
|
Daniel@0
|
54 #
|
Daniel@0
|
55 # csv_columns : string, integer, list(integer) -> list(list(string))
|
Daniel@0
|
56 def csv_columns(filename,ncols,columns):
|
Daniel@0
|
57 def getter(i): return lambda r:r[i]
|
Daniel@0
|
58 return csv_map_columns(filename,ncols,map(getter,columns))
|
Daniel@0
|
59
|
Daniel@0
|
60 # returns selected columns of CSV, one list per column, with
|
Daniel@0
|
61 # a function supplied for converting one row of strings to a row of data
|
Daniel@0
|
62 #
|
Daniel@0
|
63 # csv_map_columns : string, integer, list(list(string)->A) -> list(list(A))
|
Daniel@0
|
64 def csv_map_columns(filename,ncols,sels):
|
Daniel@0
|
65 ii=range(0,len(sels))
|
Daniel@0
|
66 data=[[] for i in ii]
|
Daniel@0
|
67 def append_columns(row):
|
Daniel@0
|
68 for i in ii: data[i].append(sels[i](row))
|
Daniel@0
|
69 csv_for_each(filename,ncols,append_columns)
|
Daniel@0
|
70
|
Daniel@0
|
71 return data
|