# CSV Helper

<figure><img src="https://291121471-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-McrRFZHYH27bqKzOVDd%2Fuploads%2FQ1ogH8t7nXQ4Re9bkL62%2Fimage.png?alt=media&#x26;token=4bee3a72-5807-4b86-96a3-273a7e682a78" alt="" width="167"><figcaption></figcaption></figure>

## Overview

With the CSV Helper, one can read, manipulate and write CSV files from URLs or other sources.

## Actions

{% hint style="warning" %}
Authentication on the CSV-Helper was **deprecated** in 2021. \
Please use a [REST Helper](https://docs.locoia.com/connectors/helpers/rest-helper) instead to fetch a file and then read it via CSV Helper.
{% endhint %}

### 1. Reading CSV file from URL location

This action reads a CSV string from a remote FTP location or HTTP URL location, including authorization tokens in headers.

Read produces a list (one entry for each row) of lists (one entry for each column), so the result of `read_csv` might look like this:

```python
[
    ["Toyota", "2022", "Silver", "Camry"],
    ["Honda", "2021", "Blue", "Accord"],
    ["Ford", "2023", "Red", "Explorer"],
    ["Chevrolet", "2020", "Black", "Impala"]
]
```

### 2. Create CSV file

This action creates a CSV string from previously generated data and saves it as a `.csv` file. The user input contains a data reference—a ref ID pointing to a list of values—and a list of column field names. These column field names are used when iterating through the data given by the ref ID to extract values from the list element. If the columns are empty, then the CSV will contain just one column, namely, the corresponding line's string representation.

{% hint style="info" %}
The input needs to be either a list of dictionaries `[{...}, {....}]` or list of lists `[[...], [...]]`

If you only have one dictionary that you would like to insert, wrap it inside a list with the [Dict Helper](https://docs.locoia.com/connectors/dict-helper#1.-dictionaries-dict), e.g. `[ {{ my_dictionary }} ]`
{% endhint %}

This action produces a CSV string and uploads it to the designated S3 bucket for storage. \
It can also be written to a file on an FTP server or sent as a mail attachment.

*Note:* If you provide a list of dictionaries where some dictionaries contain empty keys (`""`), the resulting CSV may contain `null` values for those fields, as there are no valid column headers to map the data. To avoid this, ensure all dictionaries have valid, non-empty keys when generating the CSV file.

**Example:**

1. **List of Lists:**

```json
[
    ["Name", "Age", ""],  
    ["Alice", 30, "New York"],
    ["Bob", 25, "Los Angeles"],
    ["Charlie", 35, "Chicago"]
]

# Resulting CSV might look like:

0,1,2
Name,Age,
Alice,30,New York
Bob,25,Los Angeles
Charlie,35,Chicago
```

Here, the last column has an empty header, so it is assigned a `null` value in the resulting CSV.

2. **List of Dictionaries:**

```json
[
    {"Name": "Alice", "Age": 30, "": "New York"},
    {"Name": "Bob", "Age": 25, "": "Los Angeles"},
    {"Name": "Charlie", "Age": 35, "": "Chicago"}
]

# Resulting CSV might look like:

Name,Age,
Alice,30,
Bob,25,
Charlie,35,
```

In this case, the dictionaries have an empty key (`""`), leading to empty values in the CSV output for that column.

Configuration outcome matrix:

| Print\_headers | selected\_columns  | new\_column\_names | outcome                                                                                     |
| -------------- | ------------------ | ------------------ | ------------------------------------------------------------------------------------------- |
| yes            | Some valid columns | Some valid names   | <p>If selected and new names have different length -> error<br><br>Use new column names</p> |
| yes            | Some valid columns | nothing            | Use selected column names                                                                   |
| yes            | nothing            | Some valid names   | Expect new\_colum\_names to have ONE entry                                                  |
| yes            | nothing            | nothing            | Error                                                                                       |
| no             | Some valid columns | Some valid names   | Info(Unused new names)                                                                      |
| no             | Some valid columns | nothing            | Regular csv without headers                                                                 |
| no             | nothing            | Some valid names   | Info(Unused new names)                                                                      |
| no             | nothing            | nothing            | CSV with one column and no headers                                                          |

### **3. Generate CSV from CSV text string**&#x20;

This action takes a string and converts it to CSV.\
For instance, you want to transform `"John,john@example.com,555-1234"` to CSV.&#x20;

<figure><img src="https://291121471-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-McrRFZHYH27bqKzOVDd%2Fuploads%2F9QqpDNg1hwlL16SwJMFb%2Fimage.png?alt=media&#x26;token=a7742c4a-d764-4fcc-8b01-33d324d24fec" alt=""><figcaption></figcaption></figure>

### Additional settings

### Encoding

For all CSV actions, an encoding can be selected which should be used to write/read the CSV file.\
This can be left at `utf_8` for almost all cases. This is only needed if the CSV file was saved or is expected to be saved in a specific encoding.

This is especially relevant for umlaute and special characters.

If an encoding does not support characters used in the CSV (e.g. [languages with incomplete coverage in latin-1](https://en.wikipedia.org/wiki/ISO/IEC_8859-1#Languages_with_incomplete_coverage)), the step will result in an error.

<figure><img src="https://291121471-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-McrRFZHYH27bqKzOVDd%2Fuploads%2FkR7f4WowBqGlCQIw9yMc%2Fimage.png?alt=media&#x26;token=0582c9af-3f66-4eaf-b452-8c17cf3322b3" alt=""><figcaption><p>Excerpt of encoding options</p></figcaption></figure>

The [full list of supported encodings can be seen here](https://docs.python.org/3/library/codecs.html#standard-encodings).

## Filtering a CSV file

To filter a CSV file, use [Filter List V2](https://docs.locoia.com/connectors/dict-helper#2.-filter-list-v2) from the Dict Helper

<figure><img src="https://291121471-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-McrRFZHYH27bqKzOVDd%2Fuploads%2FmvGq4tzUVB2fbyR1PFFt%2Fimage.png?alt=media&#x26;token=71524eaa-c299-4410-ba5b-6013438c001c" alt="" width="371"><figcaption></figcaption></figure>

To add a filter:

* Connect to a Dict Helper step
* Select the Filter List V2 action.&#x20;
* Fill the **List reference** input with a CSV reference without the `{{ }}`
* Set up your filter criteria

## Modifying a whole column in a CSV or dictionary

Oftentimes, formats of e.g. dates are different in every system and need to be adjusted and modified to reflect the format of a different system.&#x20;

Imagine a CSV with the column header datetime and the below three datetimestamps. That needs to be changed to only dates, like **2019-06-18**

```csv
; datetime
; 2019-06-18 23:23:23.232736734
; 2019-04-12 23:23:23.232736798
; 2017-06-11 23:23:23.232736727

```

To do the above, you need to use a combination of:

1. The connector or helper passing the object
2. A looper that changes something in every object
3. and a dict\_helper that changes a particular field in a dict
4. and a date helper that formats data in a particular way
