Exemplo n.º 1
0
    def create_table(self, data=None):
        """Create Table object from data.

        Data can be a combination of various iterable containers, e.g.
        list of lists, list of dicts, dict of lists.

        :param data:    source data for table
        """
        table = Table(data)
        self.logger.info("Created table: %s", table)
        notebook_table(table)
        return table
Exemplo n.º 2
0
    def create_table(self, data=None, trim=False):
        """Create Table object from data.

        Data can be a combination of various iterable containers, e.g.
        list of lists, list of dicts, dict of lists.

        :param data:    source data for table
        :param trim:    remove all empty rows from the end of the worksheet,
                        default `False`
        """
        table = Table(data)
        self.logger.info("Created table: %s", table)
        if trim:
            self.trim_empty_rows(table)
            self.trim_column_names(table)
        notebook_table(self.table_head(table, 10))
        return table
Exemplo n.º 3
0
    def read_table_from_csv(self,
                            path,
                            header=None,
                            columns=None,
                            dialect=None,
                            delimiters=None):
        """Read a CSV file as a table.

        By default attempts to deduce the CSV format and headers
        from a sample of the input file. If it's unable to determine
        the format automatically, the dialect and header will
        have to be defined manually.

        Valid ``dialect`` values are ``excel``, ``excel-tab``, and ``unix``,
        and ``header`` is boolean argument (``True``/``False``). Optionally a
        set of valid ``delimiters`` can be given as a string.

        The ``columns`` argument can be used to override the names of columns
        in the resulting table. The amount of columns must match the input
        data.

        :param path:        path to CSV file
        :param header:      CSV file includes header
        :param columns:     names of columns in resulting table
        :param dialect:     format of CSV file
        :param delimiters:  string of possible delimiters
        """
        sniffer = csv.Sniffer()
        with open(path, newline="") as fd:
            sample = fd.read(1024)

        if dialect is None:
            dialect = sniffer.sniff(sample, delimiters)
        if header is None:
            header = sniffer.has_header(sample)

        with open(path, newline="") as fd:
            if header:
                reader = csv.DictReader(fd, dialect=dialect)
            else:
                reader = csv.reader(fd, dialect=dialect)
            rows = list(reader)

        table = Table(rows, columns)
        notebook_table(table)
        return table
Exemplo n.º 4
0
    def read_table_from_csv(
        self,
        path,
        header=None,
        columns=None,
        dialect=None,
        delimiters=None,
        column_unknown="Unknown",
    ):
        """Read a CSV file as a table.

        :param path:            path to CSV file
        :param header:          CSV file includes header
        :param columns:         names of columns in resulting table
        :param dialect:         format of CSV file
        :param delimiters:      string of possible delimiters
        :param column_unknown:  column name for unknown fields

        By default attempts to deduce the CSV format and headers
        from a sample of the input file. If it's unable to determine
        the format automatically, the dialect and header will
        have to be defined manually.

        Valid ``dialect`` values are ``excel``, ``excel-tab``, and ``unix``,
        and ``header`` is boolean argument (``True``/``False``). Optionally a
        set of valid ``delimiters`` can be given as a string.

        The ``columns`` argument can be used to override the names of columns
        in the resulting table. The amount of columns must match the input
        data.

        If the source data has a header and rows have more fields than
        the header defines, the remaining values are put into the column
        given by ``column_unknown``. By default it has the value "Unknown".

        Examples:

        .. code-block:: robotframework

            # Source dialect is deduced automatically
            ${table}=    Read table from CSV    export.csv
            Log   Found columns: ${table.columns}

            # Source dialect is known and given explicitly
            ${table}=    Read table from CSV    export-excel.csv    dialect=excel
            Log   Found columns: ${table.columns}
        """
        sniffer = csv.Sniffer()
        with open(path, newline="") as fd:
            sample = fd.read(1024)

        if dialect is None:
            dialect = sniffer.sniff(sample, delimiters)
        if header is None:
            header = sniffer.has_header(sample)

        with open(path, newline="") as fd:
            if header:
                reader = csv.DictReader(fd,
                                        dialect=dialect,
                                        restkey=str(column_unknown))
            else:
                reader = csv.reader(fd, dialect=dialect)
            rows = list(reader)

        table = Table(rows, columns)
        notebook_table(self.table_head(table, 10))

        if header and column_unknown in table.columns:
            self.logger.warning(
                "CSV file (%s) had fields not defined in header, "
                "which can be the result of a wrong dialect",
                path,
            )

        return table