Transfer Data from CSV files to Deta Bases + Alternative solution.
Tags: database, productivity, python, deta
Published: 2020-12-04 04:20:04

## What are we going to do.

First of all , lets go through this in two phases.

* Convert csv to json
* Upload converted json to Deta base 

## Prerequisites
* Create a free account in [Deta]( https://www.deta.sh/)
* Create a new project (Note down the project key, we need it)

## Lets start.

Imagine having a csv file as
```
name,age
fredy,34
god,34345345
mannu,34
```
We want it like:
```
[{'name': 'fredy', 'age': '34'},{'name': 'god', 'age': '34345345'}
,{'name': 'maanu', 'age': '34'}
]
```

### __Lets start by importing csv module__
```python
import csv
```
Read the file contents:
```python
arr=[]
with open (path) as csvFile:
    csvReader = csv.DictReader(csvFile)
    for csvRow in csvReader:
        arr.append(csvRow)
```

* We are making a list named arr for appending all the json extracted from csv.
 
```python
csvReader = csv.DictReader(csvFile)
```

* This creates a csvreader object which maps each row to a dictionary.

* We can now use the csvreader to extract the dictionary.

```python
for csvRow in csvReader:
      arr.append(csvRow)
```

* The above loop takes each dictionary from csvreader object and append it to list arr,which i mentioned earlier.

* We can now print the list arr to see the json data.

```python
>> print(arr)
[{'name': 'fredy', 'age': '34'},{'name': 'god', 'age': '34345345'}
,{'name': 'maanu', 'age': '34'}
]
```
## __Csv file is converted , lets upload it to Deta Base__

* Install deta module.
```
pip install deta
```
* Lets import it.
```python
from deta import Deta
```

* Initialize with a Project Key( which we get while creating a project)

```python
deta = Deta(projectkey)
database = deta.Base(db)
for each in arr:
    datafile.insert(each)
    print("Uploaded {}".format(each))

```

* Connect or create a database in Deta

```python
database = deta.Base(db)
```



```python
for each in arr:
    datafile.insert(each)
    print("Uploaded {}".format(each))

```
* We are iterating through the list arr and datafile.insert(each) inserts each data into the database.

* Finally we have this in Deta Base UI.

![deta](https://raw.githubusercontent.com/fredysomy/CsvToDeta/main/images/Screenshot_20201204-000048~2.png)

### __Final Code__

```python
def csvtodeta(id,path,db):
    deta = Deta(id)
    datafile = deta.Base(db)
    print("Reading data from {}".format(path))
    arr=[]
    with open (path) as csvFile:
        csvReader = csv.DictReader(csvFile)
        for csvRow in csvReader:
           arr.append(csvRow)
    for each in arr:
        datafile.insert(each)
        print("Uploaded {}".format(each))
    print("succesfully uploaded {} data to {} Base".format(len(arr),db))

```
***


## __Alternative Solution__

I created a python CLI tool to do the same.

```python
pip install csvtodeta
```

* With just one command the data of csv file is uploaded to Deta🎉.
 
```bash
$ csvtodeta --id yourprojectkey --path path/to/csv.csv --db detabasename
```
* id is your project key from deta
* Provide the path name of csv file.
* Provide database name of the Base you want to create or connect.
* Tada...🎉🎉 The data is uploaded.

```bash
$ csvtodeta --id 45345dhsgh3rjdf2ur34hhwf --path src/deta.csv --db detabasename
Reading data from src/deta.csv
Uploaded {'name': 'fredy', 'age': '34'}
Uploaded {'name': 'god', 'age': '34345345'}
Uploaded {'name': 'maanu', 'age': '34'}
succesfully uploaded 3 data to detabasename Base

```
#### [Github Repo](https://github.com/fredysomy/CsvToDeta)

#### Thank you for reading this blog. If you have any doubts or if there is any wrong explanation please comment down here.