From e401ebc8efad1ae1c931ae89cf46dab40fb28f06 Mon Sep 17 00:00:00 2001 From: Divesh Pandey Date: Sun, 2 Apr 2017 11:26:48 +0530 Subject: [PATCH] Currently, CSV class object doesn't care about the DataType of incoming/outgoing data. This can create inconvenience for some users. For eg. if I have a numerical CSV file, the CSV._load() function still loads all data in unicode string format. This patch allows users to send Datatype also, when they are initiating a CSV object. In case no datatype is given, datatype defaults to unicode/u. --- grasp.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/grasp.py b/grasp.py index beab7ec..02437ec 100644 --- a/grasp.py +++ b/grasp.py @@ -434,12 +434,13 @@ def html(self): class CSV(matrix): - def __init__(self, name='', separator=',', rows=[]): + def __init__(self, name='', separator=',', rows=[], dtype = u): """ Returns the given .csv file as a list of rows, each a list of values. """ try: self.name = name self.separator = separator + self.dtype = dtype self._load() except IOError: pass # doesn't exist (yet) @@ -449,7 +450,7 @@ 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 = [u(v) for v in r] + r = [self.dtype(v) for v in r] self.append(r) def save(self, name=''):