I have a JSON file with data, and I am looking to see if a participant has completed a lesson by looking to see if an object called targetTitle has "Exit" where object pTitle has all the following text elements: "hard", "easy", "medium", "CD", "CH", "WU" for a given lesson.
Currently, the code cannot differentiate between the two following things:
#1: targetTitle has "Exit" and pTitle has all 6 text elements for a given lesson and participant at least once.
#2: targetTitle has "Exit" and pTitle has one of the 6 text element for a given lesson and participant at least once.
Here is example data:
[
{
"Participant": "mailto:[email protected]",
"Answer Details": {
"pTitle": "Lesson6 CH",
"targetTitle": "Exit"
}
},
{
"Participant": "mailto:[email protected]",
"Answer Details": {
"pTitle": "Lesson6 CD",
"targetTitle": "Exit"
}
},
{
"Participant": "mailto:[email protected]",
"Answer Details": {
"pTitle": "Lesson6 WU",
"targetTitle": "Exit"
}
},
{
"Participant": "mailto:[email protected]",
"Answer Details": {
"pTitle": "Lesson6 easy",
"targetTitle": "Exit"
}
},
{
"Participant": "mailto:[email protected]",
"Answer Details": {
"pTitle": "Lesson6 medium",
"targetTitle": "Exit"
}
},
{
"Participant": "mailto:[email protected]",
"Answer Details": {
"pTitle": "Lesson6 hard",
"targetTitle": "Exit"
}
},
{
"Participant": "mailto:[email protected]",
"Answer Details": {
"pTitle": "Lesson6 hard",
"targetTitle": "Exit"
}
},
{
"Participant": "mailto:[email protected]",
"Answer Details": {
"pTitle": "Lesson6 medium",
"targetTitle": "Exit"
}
},
{
"Participant": "mailto:[email protected]",
"Answer Details": {
"pTitle": "Lesson21 CH",
"targetTitle": "Exit"
}
},
{
"Participant": "mailto:[email protected]",
"Answer Details": {
"pTitle": "Lesson21 CD",
"targetTitle": "Exit"
}
},
{
"Participant": "mailto:[email protected]",
"Answer Details": {
"pTitle": "Lesson21 WU",
"targetTitle": "Exit"
}
},
{
"Participant": "mailto:[email protected]",
"Answer Details": {
"pTitle": "Lesson21 easy",
"targetTitle": "Exit"
}
},
{
"Participant": "mailto:[email protected]",
"Answer Details": {
"pTitle": "Lesson21 medium",
"targetTitle": "Exit"
}
},
{
"Participant": "mailto:[email protected]",
"Answer Details": {
"pTitle": "Lesson21 hard",
"targetTitle": "Exit"
}
}
]
Here is my current python script:
import csv
import json
import re
def check_participant(entry):
participant = entry.get('Participant', 'Participant information missing')
lessons_completed = {}
# Check if 'Answer Details' key is present
if 'Answer Details' in entry:
answer_details = entry['Answer Details']
# Get 'pTitle' and 'targetTitle' if present
p_title = answer_details.get('pTitle', 'pTitle information missing')
target_title = answer_details.get('targetTitle', 'targetTitle information missing')
# Extract lesson number using regex
lesson_number_match = re.search(r'(Lesson|L)(\d+)', p_title)
if lesson_number_match:
lesson_number = f"Lesson {lesson_number_match.group(2)}"
# Check conditions
completed = False
if any(keyword in p_title for keyword in ['WU', 'CH', 'CD', 'easy', 'medium', 'hard']) and 'Exit' in target_title:
completed = True
lessons_completed[lesson_number] = 'Yes' if completed else 'No'
return participant, lessons_completed
# Read data from JSON file
with open('example test.json', 'r') as file:
data = json.load(file)
# Process data to create rows for each participant
participant_data = {}
for entry in data:
participant, lessons_completed = check_participant(entry)
if participant not in participant_data:
participant_data[participant] = lessons_completed
else:
participant_data[participant].update(lessons_completed)
# Specify the CSV file path
csv_file_path = "Lesson Completed.csv"
# Write data to the CSV file
with open(csv_file_path, mode='w', newline='') as file:
writer = csv.writer(file)
# Write header
header = ['Participant'] + list(next(iter(participant_data.values())).keys())
writer.writerow(header)
# Write each row of data for each participant
for participant, lessons in participant_data.items():
row = [participant]
for lesson, completed in lessons.items():
row.append(completed)
writer.writerow(row)
print(f"CSV file '{csv_file_path}' has been created successfully.")
Desired output example:
Participant,Lesson 6,Lesson 21
mailto:[email protected],Yes,Yes
mailto:[email protected],No