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          """
    17          if not os.access(path, os.F_OK):
    18              raise FileNotExistError('Cannot find the csv file [%s]' % path)
    19          self._path = path
    20          self.csv2dict()
    21
    22 -    def csv2dict(self):
    23          """
    24          Convert csv data to lists, the result will be stored in data attribute
    25          """
    26          with open(self._path, 'rb') as f:
    27              rows = csv.DictReader(f)
    28              for row in rows:
    29                  self._data.append(row)
    30
    31      @property
    32 -    def data(self):
    33          """
    34          The csv data extracted from the file
    35          """
    36          return self._data
    37