import pandas as pd
import requests
from requests.auth import HTTPBasicAuth
import xmltodict
import numpy as np
# Configuration
BASE_URL = 'https://central.bbb.com/v1'
EMAIL = 'zzzz@example.com'
PASSWORD = '**********'
PROJECT_ID = '09'
FORM_ID = 'id'
CSV_PATH = '/content/drive/MyDrive/bb.csv'
# Authenticate with ODK Central API
auth = HTTPBasicAuth(EMAIL, PASSWORD)
# Read cleaned CSV dataset
cleaned_data = pd.read_csv(CSV_PATH)
# Function to handle out-of-range float values
def sanitize_floats(data):
for key, value in data.items():
if isinstance(value, float) and (np.isnan(value) or np.isinf(value)):
data[key] = str(value) # Convert NaN or inf to string
return data
# Get submissions URL
submissions_url = f'{BASE_URL}/projects/{PROJECT_ID}/forms/{FORM_ID}/submissions'
# Function to update submission
def update_submission(submission_id, submission_data):
update_url = f'{submissions_url}/{submission_id}'
response = requests.patch(update_url, json=submission_data, auth=auth)
response.raise_for_status()
print(f'Successfully updated submission {submission_id}')
return True
# Function to check if submission exists
def submission_exists(submission_id):
check_url = f'{submissions_url}/{submission_id}'
response = requests.get(check_url, auth=auth)
response.raise_for_status()
return True
# Function to fetch and print submission content
def print_submission_content(submission_id):
fetch_url = f'{submissions_url}/{submission_id}.xml'
response = requests.get(fetch_url, auth=auth)
response.raise_for_status()
submission_xml = response.content
submission_dict = xmltodict.parse(submission_xml)
print(f'Content of submission {submission_id}:')
print(submission_dict)
# Iterate through each cleaned record and update the corresponding submission in ODK Central
for index, row in cleaned_data.iterrows():
submission_id = row['KEY']
submission_data = row.to_dict()
del submission_data['KEY'] # Remove the submission_id from the data to prevent conflicts
sanitized_data = sanitize_floats(submission_data)
if submission_exists(submission_id):
try:
update_submission(submission_id, sanitized_data)
except requests.exceptions.RequestException as e:
print(f'Failed to update submission {submission_id}: {str(e)}')
else:
print(f'Submission ID {submission_id} does not exist.')
# Print the content of one submission (example: first submission from the CSV)
example_submission_id = cleaned_data.iloc[0]['KEY']
print_submission_content(example_submission_id)
print("Update process completed.")
This looks like this is at least very close to a working solution! Are you sharing it to give ideas to others or do you have a question about it?