Skip to content

CSV

Read and write CSV files.

Installation

pip install botcity-csv-plugin

Importing the Plugin

After you installed this package, the next step is to import the package into your code and start using the functions.

from botcity.plugins.csv import BotCSVPlugin\

Instantiating the Plugin

To make the example we will instantiate the plugin.

# Instantiate the plugin
bot_csv = BotCSVPlugin()

Manipulating spreadsheet data

Now, let's manipulate some data from our file, adding new data, sorting and writing the result to a new file.

# Read from a CSV File
bot_csv.read('read.csv')
# Add a row
bot_csv.add_row([0, 22])
# Sort it by columns with header H1 and H2 in descending order
bot_csv.sort(['H1', 'H2'], False)

# Print the result
print(bot_csv.as_list())
# Save it to a new file
bot_csv.write('write.csv')

Complete code

Let's take a look into the complete code:

from botcity.plugins.csv import BotCSVPlugin

# Instantiate the plugin
bot_csv = BotCSVPlugin()

# Read from a CSV File
bot_csv.read('read.csv')
# Add a row
bot_csv.add_row([0, 22])
# Sort it by columns with header H1 and H2 in descending order
bot_csv.sort(['H1', 'H2'], False)

# Print the result
print(bot_csv.as_list())
# Save it to a new file
bot_csv.write('write.csv')

Tip

This plugin allow you to use method chaining so the code above could be written as:

BotCSVPlugin().read('read.csv')
    .add_row([0, 22])
    .sort(['H1', 'H2'], False)
    .write('write.csv')