Ejemplo n.º 1
0
def read(file_name='', column=[]):
    """Read a CSV file by given header.

    :param file_name: the file name
    :type file_name: str
    :param column: the columns of CSV file
    :type column: list
    :raises: *IOError*, *csv.Error* or *ValueError* if CSV file is not readable

    :returns: a content of CSV file
    :rtype: dict
    """
    result = {}
    if not file_name:
        return result
    try:
        # Read input file
        with open(file_name, 'rb') as _csvfile:
            dialect = csv.Sniffer().sniff(_csvfile.read(), delimiters=';,')
            _csvfile.seek(0)
            reader = csv.reader(_csvfile, dialect)
            header = reader.next()
            # Header
            if len(column) == 0:
                column = header
            for h in column:
                result[h] = []
                # Content
            for row in reader:
                # Match content with origin header
                [result[h].append(row[header.index(h)]) for h in column]
    except (IOError, csv.Error, ValueError) as e:
        logging.error('%s' % e)
        return {}
    return result
Ejemplo n.º 2
0
def read(file_name='', column=[]):
    """Read a CSV file by given header.

    :param file_name: the file name
    :type file_name: str
    :param column: the columns of CSV file
    :type column: list
    :raises: *IOError*, *csv.Error* or *ValueError* if CSV file is not readable

    :returns: a content of CSV file
    :rtype: dict
    """
    result = {}
    if not file_name:
        return result
    try:
        # Read input file
        with open(file_name, 'rb') as _csvfile:
            dialect = csv.Sniffer().sniff(_csvfile.read(), delimiters=';,')
            _csvfile.seek(0)
            reader = csv.reader(_csvfile, dialect)
            header = reader.next()
            # Header
            if len(column) == 0:
                column = header
            for h in column:
                result[h] = []
                # Content
            for row in reader:
                # Match content with origin header
                [result[h].append(row[header.index(h)]) for h in column]
    except (IOError, csv.Error, ValueError) as e:
        logging.error('%s' % e)
        return {}
    return result
Ejemplo n.º 3
0
def convert(dates):
    """Converts dates to relative days.

    :param dates:
    :type array: list
    :raises: *csv.Error* or *ValueError* if CSV file is not readable

    :returns: days relative to the first date
    :rtype: array_like
    """
    res = []
    try:
        # Work with numpy array
        dates = np.array(dates)
        if dates.size < 1:
            return dates
        # the first date for the reference
        date1 = datetime.strptime(dates[0], "%Y-%m-%d")
        for date in dates:
            date2 = datetime.strptime(date, "%Y-%m-%d")
            d = date2 - date1
            res.append(d.days)
    except (TypeError, ValueError) as error:
        logging.error('Dates should have the following format: YYYY-MM-DD, %s'
                      % error)
    return np.array(res)