我有一个包含数据的 JSON 文件,我希望通过查看名为 targetTitle 的对象是否具有“Exit”来了解参与者是否已完成课程,其中对象 pTitle 具有以下所有文本元素:“hard”、“给定课程的“简单”、“中等”、“CD”、“CH”、“WU”。
目前,代码无法区分以下两件事:
#1:targetTitle 具有“退出”,pTitle 具有给定课程和参与者的全部 6 个文本元素,并且至少一次。
#2:targetTitle 具有“退出”,并且 pTitle 具有给定课程和参与者的 6 个文本元素中的 one 至少一次。
这里是示例数据:
{
"_id": "018e3ed585e09165b910c4ac",
"Score": 0,
"Participant": "[email protected]",
"Time": "2024-03-14T21:19:24.622Z",
"Answer Details": {
"pID": "df1c7ca9-7049-4c01-96f5-05b9315a9da1",
"pTitle": "L6_WU",
"iID": "daead6e2-b09a-4baa-9cbd-0d91da67a540",
"targetID": "Rad_id_1464361",
"targetTitle": "L6_WU3",
"sID": 0,
"sub": false,
"tI": 1759,
"sessionID": "ltrqs16louud421r6w",
"sessionTime": 18153,
"r": "FA"
}
},
{
"_id": "018e3ed58c6b69d81e929ba2",
"Score": 0,
"Participant": "[email protected]",
"Time": "2024-03-14T21:19:26.296Z",
"Answer Details": {
"pID": "df1c7ca9-7049-4c01-96f5-05b9315a9da1",
"pTitle": "L6_WU",
"iID": "9541df7c-d9d6-47a4-9f80-4b0d8949a86a",
"targetID": "Rad_id_1443661",
"targetTitle": "L6_WU3",
"sID": 0,
"sub": false,
"tI": 1683,
"sessionID": "ltrqs16louud421r6w",
"sessionTime": 19836,
"r": "FA"
}
},
{
"_id": "018e3ed592eca1684410c4ae",
"Score": 0,
"Participant": "[email protected]",
"Time": "2024-03-14T21:19:27.964Z",
"Answer Details": {
"pID": "df1c7ca9-7049-4c01-96f5-05b9315a9da1",
"pTitle": "L6_WU",
"iID": "edab2662-e618-485d-9226-282fd25cfaf4",
"targetID": "Rad_id_21444361",
"targetTitle": "L6_WU3",
"sID": 0,
"sub": false,
"tI": 1666,
"sessionID": "ltrqs16louud421r6w",
"sessionTime": 21502,
"r": "FA"
}
},
{
"_id": "018e3ed599b9537a6110c4b0",
"Score": 0,
"Participant": "[email protected]",
"Time": "2024-03-14T21:19:29.690Z",
"Answer Details": {
"pID": "df1c7ca9-7049-4c01-96f5-05b9315a9da1",
"pTitle": "L6_WU",
"iID": "d3af677f-2d6b-4899-958a-24c9bbe62c3b",
"targetID": "Rad_id_31444361",
"targetTitle": "L6_WU1",
"sID": 0,
"sub": false,
"tI": 1584,
"sessionID": "ltrqs16louud421r6w",
"sessionTime": 23086,
"r": "FA"
}
},
{
"_id": "018e3ed5a0918f906d929ba4",
"Score": 0,
"Participant": "[email protected]",
"Time": "2024-03-14T21:19:31.443Z",
"Answer Details": {
"pID": "df1c7ca9-7049-4c01-96f5-05b9315a9da1",
"pTitle": "L6_WU",
"iID": "26ba99a6-cf51-4893-894b-55ad1ed5db96",
"targetID": "Rad_id_69345361",
"targetTitle": "L6_WU3",
"sID": 0,
"sub": false,
"tI": 1899,
"sessionID": "ltrqs16louud421r6w",
"sessionTime": 24985,
"r": "FA"
}
},
{
"_id": "018e3ed5a6a71aef2110c4b2",
"Score": 0,
"Participant": "[email protected]",
"Time": "2024-03-14T21:19:33.013Z",
"Answer Details": {
"pID": "df1c7ca9-7049-4c01-96f5-05b9315a9da1",
"pTitle": "L6_WU",
"iID": "576bef6d-1e6c-4ac2-b1b2-875252b80620",
"targetID": "Rad_id_1440361",
"targetTitle": "L6_WU3",
"sID": 0,
"sub": false,
"tI": 1568,
"sessionID": "ltrqs16louud421r6w",
"sessionTime": 26553,
"r": "FA"
}
},
{
"_id": "018e3ed5aec9307c9a10c4b4",
"Score": 0,
"Participant": "[email protected]",
"Time": "2024-03-14T21:19:35.096Z",
"Answer Details": {
"pID": "df1c7ca9-7049-4c01-96f5-05b9315a9da1",
"pTitle": "L6_WU",
"iID": "4094b054-493c-4c0c-8993-6fd9a01a7fe6",
"targetID": "Rad_id_81440361",
"targetTitle": "L6_WU1",
"sID": 0,
"sub": false,
"tI": 1966,
"sessionID": "ltrqs16louud421r6w",
"sessionTime": 28519,
"r": "FA"
}
},
{
"_id": "018e3ed5b4378d1cfb929ba6",
"Score": 0,
"Participant": "[email protected]",
"Time": "2024-03-14T21:19:36.481Z",
"Answer Details": {
"pID": "df1c7ca9-7049-4c01-96f5-05b9315a9da1",
"pTitle": "L6_WU",
"iID": "bb845612-8c1d-4853-8151-46bace33845b",
"targetID": "Rad_id_41444361",
"targetTitle": "L6_WU3",
"sID": 0,
"sub": false,
"tI": 1500,
"sessionID": "ltrqs16louud421r6w",
"sessionTime": 30019,
"r": "FA"
}
},
{
"_id": "018e3ed5baef5391b510c4b6",
"Score": 0,
"Participant": "[email protected]",
"Time": "2024-03-14T21:19:38.203Z",
"Answer Details": {
"pID": "df1c7ca9-7049-4c01-96f5-05b9315a9da1",
"pTitle": "L6_WU",
"iID": "f0027432-dc33-4cd9-b61b-51422d617cf3",
"targetID": "Rad_id_135386",
"targetTitle": "L6_WU3 Exit",
"sID": 0,
"sub": false,
"tI": 1718,
"sessionID": "ltrqs16louud421r6w",
"sessionTime": 31737,
"r": "FA"
}
}
这是我当前的Python脚本:
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.")
所需的输出示例:
Participant,Lesson 6,Lesson 21,Lesson 23,Lesson 24,Lesson 28,Lesson 29
[email protected],Yes,Yes,No,No,No,Yes
[email protected],Yes,Yes,Yes,Yes,Yes,Yes
[email protected],Yes,Yes,No,No,No,No
[email protected],Yes,No,No,No,No,Yes
[email protected],Yes,No,No,No,No,No
[email protected],Yes,Yes,Yes,No,Yes,Yes
[email protected],Yes,No,No,No,No,No
[email protected],Yes,No,No,No,No,No
[email protected],Yes,No,No,Yes,No,No
[email protected],Yes,Yes,No,No,No,No
[email protected],Yes,No,No,No,No,No
[email protected],No,No,No,No,No,Yes
[email protected],Yes,Yes,No,Yes,Yes,Yes
[email protected],Yes,No,No,No,No,No
为了获得所需的输出并区分您提到的两种情况,您可以修改 check_participant 函数来跟踪 pTitle 中是否存在所有六个文本元素。然后,您可以相应地更新记录完成状态的方式。这是修改后的脚本:
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 if all keywords are present in pTitle
all_keywords_present = all(keyword in p_title for keyword in ['WU', 'CH', 'CD', 'easy', 'medium', 'hard'])
# Check conditions
completed = False
if all_keywords_present 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.")