Skip to content

Plugin

botcity.plugins.csv.plugin.BotCSVPlugin

header: List[str] property writable

Returns this CSV's header.

Returns:

Type Description
List[str]

The header elements in str format.

__init__(self, has_header=True, separator=',') special

This class stores the data in a CSV-like format.

Parameters:

Name Type Description Default
has_header bool

True if the CSV's first row is supposed to be the header. Defaults to True.

True
separator str

The expected separator between each field.

','

Attributes:

Name Type Description
has_header bool, Optional

A list representing the header of the CSV, if it has one. Defaults to True.

separator str, Optional

The expected separator between each field. Defaults to ','.

add_column(self, column_name=None, column=None)

Adds a new column to the CSV.

Parameters:

Name Type Description Default
column_name str, Optional

The new column's name. If None is provided, the new name will be the numeric index of the new column.

None
column List[object]

A list of csv elements. The number of elements must match the number of rows already in the list.

None

Returns:

Type Description
BotCSVPlugin

self (allows Method Chaining).

add_columns(self, columns)

Adds new columns to the CSV.

Parameters:

Name Type Description Default
columns List[List[object]]

Either a dict of columns, whose keys are the new column names and the values are lists of CSV elements, or just a list of CSV elements, in which case the column name will be it's numeric index. The number of elements of each column must match the number of rows already in the list.

required

Returns:

Type Description
BotCSVPlugin

self (allows Method Chaining).

add_row(self, row)

Adds a new row to the bottom of the CSV.

If the input contains a new column, then a new column will be created in the CSV as well, with blank fields for the previously inserted lines.

Parameters:

Name Type Description Default
row Union(List[object] or Dict[str, object]

A list of CSV elements in string format, or a dict that has the column names as its keys.

required

Returns:

Type Description
BotCSVPlugin

self (allows Method Chaining).

add_rows(self, rows)

Adds new rows to the CSV.

If the input contains a new column, then a new column will be created in the CSV as well, with blank fields for the previously inserted lines.

Parameters:

Name Type Description Default
rows List[Union[List[object], Dict[str, object]]]

A list of rows. Each row is either a list of csv elements, or a dict whose keys are the header, and the values are the new CSV elements.

required

Returns:

Type Description
BotCSVPlugin

self (allows Method Chaining).

as_dataframe(self)

Returns the contents of this CSV in a Pandas DataFrame format.

Returns:

Type Description
pandas.DataFrame

A Pandas DataFrame object.

as_dict(self)

Returns the contents of this CSV in a list of dicts format.

Returns:

Type Description
List[Dict[str, object]]

A list of rows. Each row is a dict.

as_list(self, include_header=False)

Returns the contents of this CSV in a list of lists format.

Nan values will be replaced with empty strings.

Parameters:

Name Type Description Default
include_header bool, Optional

If True, the first inner-list will receive the CSV's header. Defaults to False.

False

Returns:

Type Description
List[List[object]]

A list of rows. Each is a list of row elements.

clear(self)

Clears all the rows of the CSV, but the header remains.

Returns:

Type Description
BotCSVPlugin

self (allows Method Chaining)

get_column(self, column)

Returns the contents of an entire column in a list format.

Please note that altering the values in this list will not alter the values in the original CSV.

Parameters:

Name Type Description Default
column Union[str, int]

Either the column's name or it's 0-indexed number.

required

Returns:

Type Description
List[object]

The values of all entries within the column.

get_entry(self, column, row)

Returns the value of a single entry of a column.

Parameters:

Name Type Description Default
column Union[str, int]

Either the entry's column name or it's 0-indexed number.

required
row int

The 0-indexed row number.

required

Returns:

Type Description
object

The entry's value.

get_row(self, row)

Returns the contents of an entire row in a list format.

Please note that altering the values in this list will not alter the values in the original CSV.

Parameters:

Name Type Description Default
row int

The 0-indexed row number.

required

Returns:

Type Description
List[object]

The values of all entries within the row.

read(self, file_or_path)

Reads a CSV file using the delimiter and the has_header attributes of this class.

Parameters:

Name Type Description Default
file_or_path

Either a buffered CSV file or a path to it.

required

Returns:

Type Description
BotCSVPlugin

self (allows Method Chaining).

remove_column(self, column)

Removes single column from the CSV.

If the CSV has a header, this column will be removed from there as well.

Parameters:

Name Type Description Default
column Union[str, int]

Either the entry's column name or it's 0-indexed number.

required

Returns:

Type Description
BotCSVPlugin

self (allows Method Chaining).

remove_columns(self, columns)

Removes a list of columns from the CSV.

If the CSV has a header, this column will be removed from there as well.

Parameters:

Name Type Description Default
columns List[Union[str, int]]

A list of column names or their 0-indexed numbers.

required

Returns:

Type Description
BotCSVPlugin

self (allows Method Chaining).

remove_row(self, row)

Removes a single row from the CSV.

Keep in mind that the rows below will be moved up.

Parameters:

Name Type Description Default
row int

The 0-indexed number of the row to be removed.

required

Returns:

Type Description
BotCSVPlugin

self (allows Method Chaining).

remove_rows(self, rows)

Removes rows from the CSV.

Keep in mind that each row removed will cause the rows below it to be moved up after they are all removed.

Parameters:

Name Type Description Default
rows List[int]

A list of the 0-indexed numbers of the rows to be removed.

required

Returns:

Type Description
BotCSVPlugin

self (allows Method Chaining)

set_column(self, column, values)

Replaces the values of an entire column of the CSV.

Parameters:

Name Type Description Default
column Union[str, int]

Either the entry's column name or it's 0-indexed number.

required
values List[object]

A list of CSV elements.

required

Returns:

Type Description
BotCSVPlugin

self (allows Method Chaining)

set_entry(self, column, row, value)

Replaces the value of a single entry of a given column.

Parameters:

Name Type Description Default
column Union[str, int]

Either the entry's column name or it's 0-index number.

required
row int

The entry's 0-indexed row number.

required
value object

The new value of the entry.

required

Returns:

Type Description
BotCSVPlugin

self (allows Method Chaining)

set_row(self, row, values)

Replaces the values of an entire row of the CSV.

Parameters:

Name Type Description Default
row int

The row's 0-indexed number.

required
values Union[List[object], Dict[str, object]]

Either a list of CSV elements, or a dict whose keys are the header, and the values are the new CSV elements.

required

Returns:

Type Description
BotCSVPlugin

self (allows Method Chaining)

sort(self, by_columns, ascending=True)

Sorts the CSV rows using the first column of the by_columns parameter as a reference. In case of a tie, the second column provided is used, and so on.

Parameters:

Name Type Description Default
by_columns Union[int, str, List[Union[int, str]]]

Either a column name or its 0-indexed number; or a list of those.

required
ascending bool, Optional

Set to False if you want to use descending order. Defaults to True.

True

Returns:

Type Description
BotCSVPlugin

self (allows Method Chaining)

write(self, file_or_path)

Writes this class's CSV content to a file using it's delimiter and has_header attributes.

Parameters:

Name Type Description Default
file_or_path

Either a buffered CSV file or a path to it.

required

Returns:

Type Description
BotCSVPlugin

self (allows Method Chaining).

Back to top