

def CsvToTable(self, buf, header=True, separator=','):
    "Parses buffer into tabular format.\n\n    Strips off comments (preceded by '#').\n    Optionally parses and indexes by first line (header).\n\n    Args:\n      buf: String file buffer containing CSV data.\n      header: Is the first line of buffer a header.\n      separator: String that CSV is separated by.\n\n    Returns:\n      int, the size of the table created.\n\n    Raises:\n      TableError: A parsing error occurred.\n    "
    self.Reset()
    header_row = self.row_class()
    if header:
        line = buf.readline()
        header_str = ''
        while (not header_str):
            header_str = line.split('#')[0].strip()
            if (not header_str):
                line = buf.readline()
        header_list = header_str.split(separator)
        header_length = len(header_list)
        for entry in header_list:
            entry = entry.strip()
            if (entry in header_row):
                raise TableError(('Duplicate header entry %r.' % entry))
            header_row[entry] = entry
        header_row.row = 0
        self._table[0] = header_row
    for line in buf:
        if line.startswith('#'):
            continue
        lst = line.split(separator)
        lst = [l.strip() for l in lst]
        if (header and (len(lst) != header_length)):
            continue
        if (not header):
            header_row = self.row_class()
            header_length = len(lst)
            header_row.values = dict(zip(xrange(header_length), xrange(header_length)))
            self._table[0] = header_row
            header = True
            continue
        new_row = self.NewRow()
        new_row.values = lst
        header_row.row = (self.size + 1)
        self._table.append(new_row)
    return self.size

dataset/ETHPy150Open google/textfsm/texttable.py TextTable.CsvToTable/original