'Import CSV file (convert to JSON format) into Deta Base (database)
I try to convert the CSV file into JSON format and import it into Deta Base (database). Here is a main.py code I run from a terminal. No error shows up, but Deta Base (database) is empty not even created. Any suggestion on what is wrong with my Python script?
import csv
from deta import Deta
# Initialize with a Project Key
deta = Deta("HERE-IS-DETA-ID")
# Path to CSV file
csvFilePath = r"data.csv"
# This how to connect to or create a database
db = deta.Base("simple_db_testing")
# Create and use DB
someone = deta.Base("somewhere")
# Define conversion from CSV to JSON
def csv_to_json(csvFilePath):
    jsonArray = []
      
    # Read csv file
    with open(csvFilePath, encoding='utf-8') as csvf: 
        # Load csv file data using csv library's dictionary reader
        csvReader = csv.DictReader(csvf) 
        print(csvReader)
        # Convert each csv row into python dict
        for row in csvReader: 
            # Add this python dict to json array
            jsonArray.append(row)
            print(jsonArray)
            # Inser JsonArray into DB named "someone"
            for each in jsonArray:
                someone.put(each)
							
						Solution 1:[1]
import csv
from deta import Deta
# Initialize with a Project Key
deta = Deta("HERE-IS-DETA-ID")
# Path to CSV file
csvFilePath = r"data.csv"
# This how to connect to or create a database
db = deta.Base("simple_db_testing")
# Create and use DB
someone = deta.Base("somewhere")
# Define conversion from CSV to JSON
def csv_to_json(csvFilePath):
    # Read csv file
    with open(csvFilePath, encoding='utf-8') as csvf: 
        # Load csv file data using csv library's dictionary reader
        csvReader = csv.DictReader(csvf)
        # Convert each csv row into python dict
        for row in csvReader:
            # Insert object into DB named "someone"
            someone.put(row)
    					Solution 2:[2]
It was a missing calling function at the end of the script.
import csv
from deta import Deta
# Initialize with a Project Key
deta = Deta("HERE-IS-DETA-ID")
# Path to CSV file
csvFilePath = r"data2.csv"
# Create and use DB - this database is being used
someone = deta.Base("somewhere2")
# Define conversion from CSV to JSON
def csv_to_json(csvFilePath):
    # Read csv file
    with open(csvFilePath, encoding='utf-8') as csvf: 
        # Load csv file data using csv library's dictionary reader
        csvReader = csv.DictReader(csvf)
        # Convert each csv row into python dict
        for row in csvReader:
            # Insert object into DB named "someone"
            someone.put(row)
            # Print rows inserted into DB
            print(row)
#need to call the function
csv_to_json(csvFilePath)
    					Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source | 
|---|---|
| Solution 1 | cofob | 
| Solution 2 | ku2ck4 | 
