# Csv data ingestion to dhis2 with python

**URL:** <https://community.dhis2.org/t/csv-data-ingestion-to-dhis2-with-python/56243>\
**Category:** Support - Assistance technique\
**Created:** [17 December 2023 19:37 UTC](https://community.dhis2.org/t/csv-data-ingestion-to-dhis2-with-python/56243 "2023-12-17T19:37:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![qenehelomatjama](https://dhis2.b-cdn.net/letter_avatar_proxy/v4/letter/q/13edae/32.png) [@qenehelomatjama](https://community.dhis2.org/u/qenehelomatjama)\
**Post date:** [17 December 2023 19:37 UTC](https://community.dhis2.org/t/csv-data-ingestion-to-dhis2-with-python/56243/1 "2023-12-17T19:37:04Z")

</div>

Please help me with a working python script of loading data from csv and importing it to dhis2

---

<div class="post-metadata">

**Author:** ![Gassim](https://dhis2.b-cdn.net/user_avatar/community.dhis2.org/gassim/32/35347_2.png) [@Gassim](https://community.dhis2.org/u/Gassim)\
**Post date:** [18 December 2023 12:39 UTC](https://community.dhis2.org/t/csv-data-ingestion-to-dhis2-with-python/56243/2 "2023-12-18T12:39:53Z")

</div>

Hi @qenehelomatjama

Thank you for your post and welcome to the DHIS2 Community!

Would you add more details such as scenario and use case?

Thanks!

---

<div class="post-metadata">

**Author:** ![qenehelomatjama](https://dhis2.b-cdn.net/letter_avatar_proxy/v4/letter/q/13edae/32.png) [@qenehelomatjama](https://community.dhis2.org/u/qenehelomatjama)\
**Post date:** [19 December 2023 01:23 UTC](https://community.dhis2.org/t/csv-data-ingestion-to-dhis2-with-python/56243/3 "2023-12-19T01:23:20Z")

</div>

Thank you, we would like to automate importing of csvs outputs by running a python script into our dhis2 using dhis2 api,  
I found script in chat gpt does not work

---

<div class="post-metadata">

**Author:** ![Gassim](https://dhis2.b-cdn.net/user_avatar/community.dhis2.org/gassim/32/35347_2.png) [@Gassim](https://community.dhis2.org/u/Gassim)\
**Post date:** [20 December 2023 13:32 UTC](https://community.dhis2.org/t/csv-data-ingestion-to-dhis2-with-python/56243/4 "2023-12-20T13:32:52Z")

</div>

Would you like to share the script that didn’t work? Thanks!

---

<div class="post-metadata">

**Author:** ![qenehelomatjama](https://dhis2.b-cdn.net/letter_avatar_proxy/v4/letter/q/13edae/32.png) [@qenehelomatjama](https://community.dhis2.org/u/qenehelomatjama)\
**Post date:** [30 December 2023 11:50 UTC](https://community.dhis2.org/t/csv-data-ingestion-to-dhis2-with-python/56243/5 "2023-12-30T11:50:37Z")

</div>

import csv  
import requests  
import json

# DHIS2 API details

dhis2\_url = “[https://play.dhis2.org/2.37.0](https://play.dhis2.org/2.37.0)” # Replace with your DHIS2 server URL  
dhis2\_username = “admin”  
dhis2\_password = “district”

# CSV file path

csv\_file\_path = “C:/Users/QENEHELO MAJARA/Desktop/Routine reporting/App1 routine reporting/Output/CSVs/dataValueSets\_2022.csv”

# Read data from CSV file

data\_list =   
with open(csv\_file\_path, “r”) as csvfile:  
csv\_reader = csv.DictReader(csvfile)  
for row in csv\_reader:  
data\_list.append(row)

# DHIS2 API endpoint for data upload

dhis2\_endpoint = f"{dhis2\_url}/dataStore/your\_datastore\_name/your\_data\_key"

# Send a POST request with the data payload

response = requests.post(dhis2\_endpoint, data=json.dumps(data\_list), headers={“Content-Type”: “application/json”}, auth=(dhis2\_username, dhis2\_password))

# Check the response

if response.status\_code == 200:  
print(“Data uploaded successfully to DHIS2”)  
else:  
print(f"Failed to upload data to DHIS2. Status code: {response.status\_code}")  
print(response.text)

---

<div class="post-metadata">

**Author:** ![qenehelomatjama](https://dhis2.b-cdn.net/letter_avatar_proxy/v4/letter/q/13edae/32.png) [@qenehelomatjama](https://community.dhis2.org/u/qenehelomatjama)\
**Post date:** [12 January 2024 11:32 UTC](https://community.dhis2.org/t/csv-data-ingestion-to-dhis2-with-python/56243/6 "2024-01-12T11:32:46Z")

</div>

hi, I still need help with this script

---

<div class="post-metadata">

**Author:** ![Gassim](https://dhis2.b-cdn.net/user_avatar/community.dhis2.org/gassim/32/35347_2.png) [@Gassim](https://community.dhis2.org/u/Gassim)\
**Post date:** [16 January 2024 22:15 UTC](https://community.dhis2.org/t/csv-data-ingestion-to-dhis2-with-python/56243/7 "2024-01-16T22:15:55Z")

</div>

Hi @qenehelomatjama

I recommend testing the API request using Postman first. If you make the request using Postman, what is the response that you get?

Thanks!
