Python API
Tables
- class RPA.Tables.Tables
Tables is a library for manipulating tabular data inside Robot Framework.
It can import data from various sources and apply different operations to it. Common use-cases are reading and writing CSV files, inspecting files in directories, or running tasks using existing Excel data.
Import types
The data a table can be created from can be of two main types:
An iterable of individual rows, like a list of lists, or list of dictionaries
A dictionary of columns, where each dictionary value is a list of values
For instance, these two input values:
data1 = [ {"name": "Mark", "age": 58}, {"name": "John", "age": 22}, {"name": "Adam", "age": 67}, ] data2 = { "name": ["Mark", "John", "Adam"], "age": [ 58, 22, 67], }
Would both result in the following table:
Index
Name
Age
0
Mark
58
1
John
22
2
Adam
67
Indexing columns and rows
Columns can be referred to in two ways: either with a unique string name or their position as an integer. Columns can be named either when the table is created, or they can be (re)named dynamically with keywords. The integer position can always be used, and it starts from zero.
For instance, a table with columns “Name”, “Age”, and “Address” would allow referring to the “Age” column with either the name “Age” or the number 1.
Rows do not have a name, but instead only have an integer index. This index also starts from zero. Keywords where rows are indexed also support negative values, which start counting backwards from the end.
For instance, in a table with five rows, the first row could be referred to with the number 0. The last row could be accessed with either 4 or -1.
Examples
Robot Framework
The Tables library can load tabular data from various other libraries and manipulate it inside Robot Framework.
*** Settings *** Library RPA.Tables *** Keywords *** Files to Table ${files}= List files in directory ${CURDIR} ${files}= Create table ${files} Filter table by column ${files} size >= ${1024} FOR ${file} IN @{files} Log ${file}[name] END Write table to CSV ${files} ${OUTPUT_DIR}${/}files.csv
Python
The library is also available directly through Python, where it is easier to handle multiple different tables or do more bespoke manipulation operations.
from RPA.Tables import Tables library = Tables() orders = library.read_table_from_csv( "orders.csv", columns=["name", "mail", "product"] ) customers = library.group_table_by_column(rows, "mail") for customer in customers: for order in customer: add_cart(order) make_order()
- ROBOT_LIBRARY_DOC_FORMAT = 'REST'
- ROBOT_LIBRARY_SCOPE = 'GLOBAL'
- add_table_column(table: Table, name: Optional[str] = None, values: Optional[Any] = None)
Append a column to a table.
- Parameters
table – Table to modify
name – Name of new column
values – Value(s) for new column
The
values
can either be a list of values, one for each row, or one single value that is set for all rows.Examples:
# Add empty column Add table column ${table} # Add empty column with name Add table column ${table} name=Home Address # Add new column where every every row has the same value Add table column ${table} name=TOS values=${FALSE} # Add new column where every row has a unique value ${is_first}= Create list ${TRUE} ${FALSE} ${FALSE} Add table column ${table} name=IsFirst values=${is_first}
- add_table_row(table: Table, values: Optional[Any] = None)
Append rows to a table.
- Parameters
table – Table to modify
values – Value(s) for new row
The
values
can either be a list of values, or a dictionary where the keys match current column names. Values for unknown keys are discarded.It can also be a single value that is set for all columns, which is
None
by default.Examples:
# Add empty row Add table row ${table} # Add row where every column has the same value Add table row ${table} Unknown # Add values per column ${values}= Create dictionary Username=Mark Mail=mark@robocorp.com Add table row ${table} ${values}
- clear_table(table: Table)
Clear table in-place, but keep columns.
- Parameters
table – Table to clear
Example:
Clear table ${table}
- copy_table(table: Table) Table
Make a copy of a table object.
- Parameters
table – Table to copy
- Returns
Table object
${table_copy}= Copy table ${table}
- create_table(data: Optional[Union[Dict[Union[int, str], Union[Dict, List, Tuple, NamedTuple, set]], List[Union[Dict, List, Tuple, NamedTuple, set]], Table]] = None, trim: bool = False, columns: Optional[List[str]] = None) Table
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.
- Parameters
data – Source data for table
trim – Remove all empty rows from the end of the worksheet, default False
columns – Names of columns (optional)
- Returns
Table object
See the main library documentation for more information about supported data types.
Example:
# Create a new table using a Dictionary of Lists # Because of the dictionary keys the column names will be automatically set @{Table_Data_name}= Create List Mark John Amy @{Table_Data_age}= Create List ${58} ${22} ${67} &{Table_Data}= Create Dictionary ... name=${Table_Data_name} ... age=${Table_Data_age} ${table}= Create Table ${Table_Data}
- export_table(table: Table, with_index: bool = False, as_list: bool = True) Union[List, Dict]
Convert a table object into standard Python containers.
- Parameters
table – Table to convert to dict
with_index – Include index in values
as_list – Export data as list instead of dict
- Returns
A List or Dictionary that represents the table
Example:
${orders}= Read worksheet as table orders.xlsx Sort table by column ${orders} CustomerId ${export}= Export table ${orders} # The following keyword expects a dictionary: Write as JSON ${export}
- filter_empty_rows(table: Table)
Remove all rows from a table which have only
None
values.- Parameters
table – Table to filter
The filtering will be done in-place.
Example:
Filter empty rows ${table}
- filter_table_by_column(table: Table, column: Union[int, str], operator: str, value: Any)
Remove all rows where column values don’t match the given condition.
- Parameters
table – Table to filter
column – Column to filter with
operator – Filtering operator, e.g. >, <, ==, contains
value – Value to compare column to (using operator)
See the keyword
Find table rows
for all supported operators and their descriptions.The filtering will be done in-place.
Examples:
# Only accept prices that are non-zero Filter table by column ${table} price != ${0} # Remove uwnanted product types @{types}= Create list Unknown Removed Filter table by column ${table} product_type not in ${types}
- filter_table_with_keyword(table: Table, name: str, *args)
Run a keyword for each row of a table, then remove all rows where the called keyword returns a falsy value.
Can be used to create custom RF keyword based filters.
- Parameters
table – Table to modify.
name – Keyword name used as filter.
args – Additional keyword arguments to be passed. (optional)
The row object will be given as the first argument to the filtering keyword.
- find_table_rows(table: Table, column: Union[int, str], operator: str, value: Any)
Find all the rows in a table which match a condition for a given column.
- Parameters
table – Table to search into.
column – Name or position of the column to compare with.
operator – Comparison operator used with every cell value on the specified column.
value – Value to compare against.
- Returns
New Table object containing all the rows matching the condition.
Supported operators:
Operator
Description
>
Cell value is larger than
<
Cell value is smaller than
>=
Cell value is larger or equal than
<=
Cell value is smaller or equal than
==
Cell value is equal to
!=
Cell value is not equal to
is
Cell value is the same object
not is
Cell value is not the same object
contains
Cell value contains given value
not contains
Cell value does not contain given value
in
Cell value is in given value
not in
Cell value is not in given value
Returns the matches as a new Table instance.
Examples:
# Find all rows where price is over 200 @{rows} = Find table rows ${table} Price > ${200} # Find all rows where the status does not contain "removed" @{rows} = Find table rows ${table} Status not contains removed
- get_table_cell(table: Table, row: Union[int, str], column: Union[int, str]) Any
Get a cell value from a table.
- Parameters
table – Table to read from
row – Row of cell
column – Column of cell
- Returns
Cell value
Examples:
# Get the value in the first row and first column Get table cell ${table} 0 0 # Get the value in the last row and first column Get table cell ${table} -1 0 # Get the value in the last row and last column Get table cell ${table} -1 -1 # Get the value in the third row and column "Name" Get table cell ${table} 2 Name
- get_table_column(table: Table, column: Union[int, str]) List
Get all values for a single column in a table.
- Parameters
table – Table to read
column – Column to read
- Returns
List of the rows in the selected column
Example:
${emails}= Get table column ${users} E-Mail Address
- get_table_dimensions(table: Table) Tuple[int, int]
Return table dimensions, as (rows, columns).
- Parameters
table – Table to inspect
- Returns
Two integer values that represent the number of rows and columns
Example:
${rows} ${columns}= Get table dimensions ${table} Log Table has ${rows} rows and ${columns} columns.
- get_table_row(table: Table, row: Union[int, str], as_list: bool = False) Union[Dict, List]
Get a single row from a table.
- Parameters
table – Table to read
row – Row to read
as_list – Return list instead of dictionary
- Returns
Dictionary or List of table row
Examples:
# returns the first row in the table ${first}= Get table row ${orders} # returns the last row in the table ${last}= Get table row ${orders} -1 as_list=${TRUE}
- get_table_slice(table: Table, start: Optional[Union[int, str]] = None, end: Optional[Union[int, str]] = None) Union[Table, List[List]]
Return a new Table from a range of given Table rows.
- Parameters
table – Table to read from
start – Start index (inclusive)
start – End index (exclusive)
- Returns
Table object of the selected rows
If
start
is not defined, starts from the first row. Ifend
is not defined, stops at the last row.Examples:
# Get all rows except first five ${slice}= Get table slice ${table} start=5 # Get rows at indexes 5, 6, 7, 8, and 9 ${slice}= Get table slice ${table} start=5 end=10 # Get all rows except last five ${slice}= Get table slice ${table} end=-5
- group_table_by_column(table: Table, column: Union[int, str]) List[Table]
Group a table by
column
and return a list of grouped Tables.- Parameters
table – Table to use for grouping
column – Column which is used as grouping criteria
- Returns
List of Table objects
Example:
# Groups rows of matching customers from the `customer` column # and returns the groups or rows as Tables @{groups}= Group table by column ${orders} customer # An example of how to use the List of Tables once returned FOR ${group} IN @{groups} # Process all orders for the customer at once Process order ${group} END
- map_column_values(table: Table, column: Union[int, str], name: str, *args)
Run a keyword for each cell in a given column, and replace its content with the return value.
Can be used to easily convert column types or values in-place.
- Parameters
table – Table to modify.
column – Column to modify.
name – Mapping keyword name.
args – Additional keyword arguments. (optional)
The cell value will be given as the first argument to the mapping keyword.
Examples:
# Convert all columns values to a different type Map column values ${table} Price Convert to integer # Look up values with a custom keyword Map column values ${table} User Map user ID to name
- merge_tables(*tables: Table, index: Optional[str] = None) Table
Create a union of two tables and their contents.
- Parameters
tables – Tables to merge
index – Column name to use as index for merge
- Returns
Table object
By default rows from all tables are appended one after the other. Optionally a column name can be given with
index
, which is used to merge rows together.Example:
For instance, a
name
column could be used to identify unique rows and the merge operation should overwrite values instead of appending multiple copies of the same name.Name
Price
Egg
10.0
Cheese
15.0
Ham
20.0
Name
Stock
Egg
12.0
Cheese
99.0
Ham
0.0
${products}= Merge tables ${prices} ${stock} index=Name FOR ${product} IN @{products} Log many ... Product: ${product}[Name] ... Price: ${product}[Price] ... Stock: ${product}[Stock] END
- pop_table_column(table: Table, column: Optional[Union[int, str]] = None) Union[Dict, List]
Remove column from table and return it.
- Parameters
table – Table to modify
column – Column to remove
- Returns
Dictionary or List of the removed, popped, column
Examples:
# Remove column from table and discard it Pop table column ${users} userId # Remove column from table and iterate over it ${ids}= Pop table column ${users} userId FOR ${id} IN @{ids} Log User id: ${id} END
- pop_table_row(table: Table, row: Optional[Union[int, str]] = None, as_list: bool = False) Union[Dict, List]
Remove row from table and return it.
- Parameters
table – Table to modify
row – Row index, pops first row if none given
as_list – Return list instead of dictionary
- Returns
Dictionary or List of the removed, popped, row
Examples:
# Pop the firt row in the table and discard it Pop table row ${orders} # Pop the last row in the table and store it ${row}= Pop table row ${data} -1 as_list=${TRUE}
- read_table_from_csv(path: str, header: Optional[bool] = None, columns: Optional[List[str]] = None, dialect: Optional[Union[str, Dialect]] = None, delimiters: Optional[str] = None, column_unknown: str = 'Unknown', encoding: Optional[str] = None) Table
Read a CSV file as a table.
- Parameters
path – Path to CSV file
header – CSV file includes header
columns – Names of columns in resulting table
dialect – Format of CSV file
delimiters – String of possible delimiters
column_unknown – Column name for unknown fields
encoding – Text encoding for input file, uses system encoding by default
- Returns
Table object
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.
Builtin
dialect
values areexcel
,excel-tab
, andunix
, andheader
is boolean argument (True
/False
). Optionally a set of validdelimiters
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:
# 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}
- rename_table_columns(table: Table, names: List[Optional[str]], strict: bool = False)
Renames columns in the Table with given values. Columns with name as
None
will use the previous value.- Parameters
table – Table to modify
names – List of new column names
strict – If True, raises ValueError if column lengths do not match
The renaming will be done in-place.
Examples:
# Initially set the column names ${columns}= Create list First Second Third Rename table columns ${table} ${columns} # First, Second, Third # Update the first and second column names to Uno and Dos ${columns}= Create list Uno Dos Rename table columns ${table} ${columns} # Uno, Dos, Third
- set_row_as_column_names(table: Table, row: Union[int, str])
Set existing row as names for columns.
- Parameters
table – Table to modify
row – Row to use as column names
Example:
# Set the column names based on the first row Set row as column names ${table} 0
- set_table_cell(table: Table, row: Union[int, str], column: Union[int, str], value: Any)
Set a cell value in a table.
- Parameters
table – Table to modify to
row – Row of cell
column – Column of cell
value – Value to set
Examples:
# Set the value in the first row and first column to "First" Set table cell ${table} 0 0 First # Set the value in the last row and first column to "Last" Set table cell ${table} -1 0 Last # Set the value in the last row and last column to "Corner" Set table cell ${table} -1 -1 Corner # Set the value in the third row and column "Name" to "Unknown" Set table cell ${table} 2 Name Unknown
- set_table_column(table: Table, column: Union[int, str], values: Any)
Assign values to a column in the table.
- Parameters
table – Table to modify
column – Column to modify
values – Value(s) to set
The
values
can either be a list of values, one for each row, or one single value that is set for all rows.Examples:
# Set different value for each row (sizes must match) ${ids}= Create list 1 2 3 4 5 Set table column ${users} userId ${ids} # Set the same value for all rows Set table column ${users} email ${NONE}
- set_table_row(table: Table, row: Union[int, str], values: Any)
Assign values to a row in the table.
- Parameters
table – Table to modify
row – Row to modify
values – Value(s) to set
The
values
can either be a list of values, or a dictionary where the keys match current column names. Values for unknown keys are discarded.It can also be a single value that is set for all columns.
Examples:
${columns}= Create list One Two Three ${table}= Create table columns=${columns} ${values}= Create list 1 2 3 Set table row ${table} 0 ${values} ${values}= Create dictionary One=1 Two=2 Three=3 Set table row ${table} 1 ${values} Set table row ${table} 2 ${NONE}
- sort_table_by_column(table: Table, column: Union[int, str], ascending: bool = True)
Sort a table in-place according to
column
.- Parameters
table – Table to sort
column – Column to sort with
ascending – Table sort order
Examples:
# Sorts the `order_date` column ascending Sort table by column ${orders} order_date # Sorts the `order_date` column descending Sort table by column ${orders} order_date ascending=${FALSE}
- table_head(table: Table, count: int = 5, as_list: bool = False) Union[Table, List[List]]
Return first
count
rows from a table.- Parameters
table – Table to read from
count – Number of lines to read
as_list – Return list instead of Table
- Returns
Return Table object or List of the selected rows
Example:
# Get the first 10 employees ${first}= Table head ${employees} 10
- table_tail(table: Table, count: int = 5, as_list: bool = False) Union[Table, List[List]]
Return last
count
rows from a table.- Parameters
table – Table to read from
count – Number of lines to read
as_list – Return list instead of Table
- Returns
Return Table object or List of the selected rows
Example:
# Get the last 10 orders ${latest}= Table tail ${orders} 10
- trim_column_names(table: Table)
Remove all extraneous whitespace from column names.
- Parameters
table – Table to filter
The filtering will be done in-place.
Example:
# This example will take colums such as: # "One", "Two ", " Three " # and trim them to become the below: # "One", "Two", "Three" Trim column names ${table}
- trim_empty_rows(table: Table)
Remove all rows from the end of a table which have only
None
as values.- Parameters
table – Table to filter
The filtering will be done in-place.
Example:
Trim empty rows ${table}
- write_table_to_csv(table: Table, path: str, header: bool = True, dialect: Union[str, Dialect] = Dialect.Excel, encoding: Optional[str] = None, delimiter: Optional[str] = ',')
Write a table as a CSV file.
- Parameters
table – Table to write
path – Path to write to
header – Write columns as header to CSV file
dialect – The format of output CSV
encoding – Text encoding for output file, uses system encoding by default
delimiter – Delimiter character between columns
Builtin
dialect
values areexcel
,excel-tab
, andunix
.Example:
${sheet}= Read worksheet as table orders.xlsx header=${TRUE} Write table to CSV ${sheet} output.csv