Esempio n. 1
0
class CSVUtil(object):
  
  def __init__(self):
    self._fLoader = PyFileLoader()
  #@Arguments:
    #path = path string for a csv file 
    #keyIndex = an interger with the column index to become the dictionary key, set as 0 by default, i.e.: first column of csv file
  #Return: A dictionary whose keys were set by keyIndex and the value is a list of dictionaries for the specified key
  def loadCSV(self, path, keyIndex=0):
    path = path.lstrip("/").rstrip("\n")
    collection = {}
    try:
      data = self._fLoader.loadPath(path)
      n = 0
      header = ""
      for row in data:
        info = row.rstrip("\n").split(";")
        #Getting value as the key for the dictionary
        key = info.pop(keyIndex)     
        #Reading the header
        if n == 0:
          header = info
          n = n + 1
        #For the rest of the file
        else:
          single = {}
          i = 0
          for col in header:
            single[col] = info[i]
            i = i+1
          self._fLoader.pushDict(collection, key, single)
    except Exception, e:
      print e
    return collection