'Write 2D array to CSV file or return the string if no filename is given'

def writeCsv(self, fileName=None):

    if (self.ndim > 2):
        raise Exception('CSV    Export    is    only    for    2D    arrays')
    if (fileName is not None):
        file = open(fileName, 'w')
    ret = ''
    if ('cols' in self._info[0]):
        s = (','.join([x['name'] for x in self._info[0]['cols']]) + '\n')
        if (fileName is not None):
            file.write(s)
        else:
            ret += s
    for row in range(0, self.shape[1]):
        s = (','.join([('%g' % x) for x in self[:, row]]) + '\n')
        if (fileName is not None):
            file.write(s)
        else:
            ret += s
    if (fileName is not None):
        file.close()
    else:
        return ret
