From 8601dde0034f8778c8ed6c216b3c493d0c8f8db5 Mon Sep 17 00:00:00 2001 From: Divesh Pandey Date: Mon, 3 Apr 2017 10:41:12 +0530 Subject: [PATCH] Support for number of columns in CSV class --- grasp.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/grasp.py b/grasp.py index beab7ec..68e1334 100644 --- a/grasp.py +++ b/grasp.py @@ -434,12 +434,15 @@ def html(self): class CSV(matrix): - def __init__(self, name='', separator=',', rows=[]): + def __init__(self, name='', separator=',', rows=[], n_cols=None): """ Returns the given .csv file as a list of rows, each a list of values. """ try: self.name = name self.separator = separator + # n_cols stores number of initial columns to be used in CSV matrix + # Default value is None, later is set to maximum number of cols. + self.n_cols = n_cols self._load() except IOError: pass # doesn't exist (yet) @@ -449,8 +452,10 @@ def __init__(self, name='', separator=',', rows=[]): def _load(self): with open(self.name, 'r') as f: for r in csvlib.reader(f, delimiter=self.separator): + r = r[:self.n_cols] if self.n_cols else r r = [u(v) for v in r] self.append(r) + self.n_cols = len(self[0]) def save(self, name=''): a = []