Import Data with the Python Client

Open inAnthropic

Prerequisites

  • TerminusDB running locally — see Docker setup for instructions
  • The TerminusDB Python client installed (installation guide)
  • A database with a schema defined
  • Data to import (CSV, JSON, or DataFrame)

What you'll achieve By the end of this guide, you will have imported data from an external source into your TerminusDB database using the Python client.

This how-to assumes that you are already connected to a database and have a schema that matches the CSV you want to import.

Importing a CSV file

You can import CSV files easily by importing them into dictionaries using Python's built-in libraries. Those dictionary objects can be inserted into the database using the insert_document function.

Example: Python
import csv
objects = []
with open('test.csv', 'r') as f:
    csv_reader = csv.DictReader(f)
    objects = list(csv_reader)

# Add @type to each row to match your schema class
for obj in objects:
    obj['@type'] = 'YourClassName'

client.insert_document(objects)

If your database has only one document class, TerminusDB can infer the @type automatically. When you have multiple classes, you must set @type on each document explicitly.

Next steps

Was this helpful?