# Import CSV file using pystardog stardog.content.ImportFile

**URL:** https://community.stardog.com/t/import-csv-file-using-pystardog-stardog-content-importfile/4225
**Category:** Support
**Created:** [January 31, 2023, 3:39pm UTC](https://community.stardog.com/t/import-csv-file-using-pystardog-stardog-content-importfile/4225 "2023-01-31T15:39:00Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![ulrichschur](https://avatars.discourse-cdn.com/v4/letter/u/5f9b8f/32.png) [@ulrichschur](https://community.stardog.com/u/ulrichschur)
#### Post date: [January 31, 2023, 3:39pm UTC](https://community.stardog.com/t/import-csv-file-using-pystardog-stardog-content-importfile/4225/1 "2023-01-31T15:39:00Z")

</div>

I am trying a simple **CSV file import** with pystardog/Python, but am stuck with **stardog.exceptions.StardogException: [415]**.

```auto
import stardog

connection_details = {
  'endpoint': 'myURLtoStardog',
  'username': 'myusername',
  'password': 'myuserpwd',
  'database': 'mystardogdbname'
}

# Connect to DB
with stardog.Connection(**connection_details) as connection:

    connection.begin()
    
    # import CSV file using a SMS2 mapping file
    connection.add(stardog.content.ImportFile('cars.csv'))
    connection.add(stardog.content.MappingFile('cars_mappings.sms'))
    
    connection.commit()

```

I am using the official [Stardog example csv files](https://github.com/stardog-union/stardog-examples/tree/develop/examples/cli/virtual/csv). I am following the syntax/examples described in the [pystardog documentation](https://pystardog.readthedocs.io/en/0.13.0/source/stardog.html#stardog.content.ImportFile). But I end up with below error.

Has anyone an idea what I am doing wrong? Or is my assumption incorrect that I can use pystardog to do the same [csv import as with the Stardog CLI](https://docs.stardog.com/virtual-graphs/importing-json-csv-files#importing-csv-files)?

```auto
Traceback (most recent call last):
  File "C:\Users\me\stardog_import-csv.py", line 16, in <module>
    connection.add(stardog.content.ImportFile('cars.csv'))
  File "C:\Users\me\AppData\Roaming\Python\Python39\site-packages\stardog\connection.py", line 165, in add
    self.client.post("/{}/add".format(self.transaction), **args)
  File "C:\Users\me\AppData\Roaming\Python\Python39\site-packages\stardog\http\client.py", line 49, in post
    return self.__wrap(self.session.post(self.url + path, **kwargs))
  File "C:\Users\me\AppData\Roaming\Python\Python39\site-packages\stardog\http\client.py", line 71, in __wrap
    raise exceptions.StardogException(
stardog.exceptions.StardogException: [415] :

```

---

<div class="post-metadata">

### Author: ![mike](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/mike/32/14_2.png) [@mike](https://community.stardog.com/u/mike)
#### Post date: [January 31, 2023, 3:57pm UTC](https://community.stardog.com/t/import-csv-file-using-pystardog-stardog-content-importfile/4225/2 "2023-01-31T15:57:29Z")

</div>

Here's an [example](https://github.com/stardog-union/pystardog/blob/main/test/test_admin_basic.py#L493-L501) in the pystardog source code.

---

<div class="post-metadata">

### Author: ![ulrichschur](https://avatars.discourse-cdn.com/v4/letter/u/5f9b8f/32.png) [@ulrichschur](https://community.stardog.com/u/ulrichschur)
#### Post date: [February 1, 2023, 3:16pm UTC](https://community.stardog.com/t/import-csv-file-using-pystardog-stardog-content-importfile/4225/3 "2023-02-01T15:16:44Z")

</div>

Thanks for the quick reply, Michael.

The issue isn't with stardog.content.ImportFile - this is working fine I think. The problem seems to be with **connection.add** , at least the server doesn't seems to to accept the csv file and provides a return code **415 = unsupported media type**.

By the way, the example you provided is for stardog. **admin.import\_file** , a different functions. But I also find an example for stardog. **content.ImportFile** [here](https://github.com/stardog-union/pystardog/blob/0.13.0/test/test_unit.py#L377).

---

<div class="post-metadata">

### Author: ![mike](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/mike/32/14_2.png) [@mike](https://community.stardog.com/u/mike)
#### Post date: [February 1, 2023, 3:39pm UTC](https://community.stardog.com/t/import-csv-file-using-pystardog-stardog-content-importfile/4225/4 "2023-02-01T15:39:45Z")

</div>

> The problem seems to be with **connection.add**

That's correct. That's the wrong function to call. That's why I pointed you to the right function to use.

Regarding the example you pointed you, you'll notice that it's not a test for import, its testing content type detection. The example I provided is the most relevant example for you to follow.

---

<div class="post-metadata">

### Author: ![ulrichschur](https://avatars.discourse-cdn.com/v4/letter/u/5f9b8f/32.png) [@ulrichschur](https://community.stardog.com/u/ulrichschur)
#### Post date: [February 2, 2023, 1:02pm UTC](https://community.stardog.com/t/import-csv-file-using-pystardog-stardog-content-importfile/4225/5 "2023-02-02T13:02:00Z")

</div>

You were right, Mike. With those hints, I finally got it working. Many thanks!

Here is my working code. A great and simple alternative to the CLI command "[stardog-admin virtual import](https://docs.stardog.com/stardog-admin-cli-reference/virtual/virtual-import)" to import a CSV file with a [SMS2 mapping file](https://docs.stardog.com/virtual-graphs/mapping-data-sources#sms2-stardog-mapping-syntax-2).

```auto
import stardog

admin = stardog.Admin(
        endpoint='myURLtoStardog:port',
        username='myusername',
        password='myuserpwd'
)

admin.import_file(
        'mystardogdbname',
        stardog.content.MappingFile('cars_mappings.sms'),
        stardog.content.ImportFile('cars.csv')
)

```
