Package tlib :: Package base :: Module CsvModerator
[hide private]
[frames] | no frames]

Source Code for Module tlib.base.CsvModerator

 1  import os 
 2  import csv 
 3  from ExceptionHelper import FileNotExistError 
4 5 6 -class CsvHelper(object):
7 8 _data = [] 9 _path = None 10
11 - def __init__(self, path):
12 """ 13 Constructor for class 14 15 @param path : path to the excel file 16 @param isFirstRowHeader : is the first row of the excel file a header row? 17 If true, the return result for reach row will be dictionary, otherwise, it will be a list 18 """ 19 if not os.access(path, os.F_OK): 20 raise FileNotExistError('Cannot find the csv file [%s]' % path) 21 self._path = path 22 self.csv2dict()
23
24 - def csv2dict(self):
25 """ 26 Convert csv data to lists, the result will be stored in data attribute 27 """ 28 with open(self._path, 'rb') as f: 29 rows = csv.DictReader(f) 30 for row in rows: 31 self._data.append(row)
32 33 @property
34 - def data(self):
35 """ 36 The csv data extracted from the file 37 """ 38 return self._data
39