Please help me with a working python script of loading data from csv and importing it to dhis2
Thank you for your post and welcome to the DHIS2 Community!
Would you add more details such as scenario and use case?
Thanks!
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
Would you like to share the script that didn’t work? Thanks!
import csv
import requests
import json
DHIS2 API details
dhis2_url = “DHIS 2 Demo - Sierra Leone” # 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)
hi, I still need help with this script
I recommend testing the API request using Postman first. If you make the request using Postman, what is the response that you get?
Thanks!