import array, csv, datetime with open(r'C:\Users\Scottpeterson2\Documents\DevMath\Test Automation\Detailed_Test_Results.csv', newline='') as pearson_csvfile: preader = csv.DictReader(pearson_csvfile) pearson_data = [] # Creating Pearson Data Array of Dictionaries for row in preader: if row['Date/Time Worked'] == '': p_date_set = '' else: p_date_set = datetime.datetime.strptime(row['Date/Time Worked'], '%m/%d/%y %I:%M%p') p_date_set_updated = datetime.datetime.strftime(p_date_set, '%m/%d/%y %I:%M%p') row['Date/Time Worked'] = p_date_set_updated row["Last name"] = row["Last name"].lower() row["First name"] = row["First name"].lower() pearson_data.append(row) #print(row) with open(r'C:\Users\Scottpeterson2\Documents\DevMath\Test Automation\Math 0950 Test 1.csv', newline='') as chitester_csvfile: creader = csv.DictReader(chitester_csvfile) chitester_data = [] # Creating C Data Array of Dictionaries for row in creader: c_name_splitter = row['Student Name'].split(", ") c_date_set_start = datetime.datetime.strptime(row['Start Time'], '%m/%d/%Y %H:%M') row['Start Time'] = c_date_set_start try: c_date_set_time = datetime.datetime.strptime(row['End Time'], '%I:%M %p') c_date_set_end = c_date_set_start c_date_set_end = c_date_set_end.replace(hour=c_date_set_time.hour,minute=c_date_set_time.minute) row['Date/Time Worked'] = c_date_set_end except: c_date_set_end = datetime.datetime.strptime(row['End Time'], '%m/%d/%Y %H:%M') row['End Time'] = c_date_set_end row['Date/Time Worked'] = datetime.datetime.strftime(c_date_set_end, '%m/%d/%y %I:%M%p') row["Last name"] = c_name_splitter[0].lower() row["First name"] = c_name_splitter[1].lower() chitester_data.append(row) #print(row) # This is the Pearson array of dictionaries I created from the Pearson csv file above. # Each row contains an a dictionary that was built from a single row of the csv file. # The important fields we are comparing are row['Last name'], row['First name'], and row['Date/Time Worked']. # There rows with blank times for the tests - those can be ingnored. # This is the smaller of the two lists as it is just the results for a single test do we want to iterate through this list and compare each # test result with a date and time with the chitester rows and see if there is a matching row['Last name'] AND row['First name']. # At that point, if there is a matching Last and First name, then we want to compare the row ['Date/Time Worked']fields and see if it is # on the same date and with in a 15min time difference. for row in pearson_data: #testing to see what it outputed - can also be changed to chitester_data if row['Date/Time Worked'] == '': print('This test time blank.') else: print(row['First name'],row['Last name'],row['Date/Time Worked']) # create loop to go through pearson_data array of dictionaries # in loop check first row for last name and test time - possibly set variable for testing? *if test time is blank - then skip # now check each row in chitester_data array for matching set of last name and test time (will probably also need to check first name) # for those names that match - then check the date to find the same date # for those with a matching date then then check time # any with a test time with a greater difference than 15mins plus or minus - write out to new .csv file - output results # this is the part I need help starting... I'm thinking a couple nested for loops with some comparison checks and it shouldn't be that hard # but I'm dumb and having a hard time starting it - below is my attempt to start it in pseudo code for row in pearson_data: if row['Date/Time Worked'] == '': # skip this row else: # now I need to interate chitester_data rows and compare last and first names to the current last and first name in pearson_data # if there is not match - we can end this row in pearson_data with No Match Found in Chitester - move on to the next row in pearson_data # if there is a match - compare Date/Time Worked # if no matching date - we can end this row in pearson_data with No dates matched in Chitester - move on to the next row in pearson_data # if there is a matching date - we can compare the time # if it's within the 15mins +/- limit no need to return anything - we can end the row check - move on to the next row in pearson_data # if it's not within the 15min +/- limit - append this user's first and last name and test times to a new array to be written # to a csv to - then move on the next row in pearson_data and continue until all rows are complete
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question