I was using this code to seperate max and min values and it worked for the first few times. but afterwards when debugging it threw up Exception Unhandled: name "minimum" is not defined. Any bit of help would be much appreciated, the code was not written by myself but my friend cant help me out until monday and i was hoping to finish up with what i was doing before then.
import csv
# Function to split list l in to n-sized chunks
def chunks(l, n):
for i in range(0, len(l), n):
yield l[i:i + n]
# Imports the .csv file
# Note: File must be in same directory as script and named 'data.csv' unless the line below is changed
with open('data.csv') as csvfilein:
datareader = csv.reader(csvfilein, delimiter=',', quotechar='|')
data = []
for row in datareader:
data.append(row)
data_intervals = chunks(data, 10) # Split array 'data' into intervals of 10 tuples
# CSV writeback to file 'out.csv' in the same folder as script
with open('out.csv', 'wt') as csvfileout:
# Initialize writer
datawriter = csv.writer(csvfileout, delimiter=',', quoting=csv.QUOTE_MINIMAL, lineterminator='\n')
# Write headings to file
datawriter.writerow(['Interval #', '', 'Min force (kN)', 'Distance (mm)', '', 'Max force (kN)', 'Distance (mm)'])
interval_count = 1 # Current interval count
for current_interval in data_intervals: # Iterate through each interval
# Calculate the min and max values in the current interval based on the SECOND value (force, not distance)
# This is what the lambda function is for
try:
minimum = min(current_interval, key=lambda t: int(t[1]))
maximum = max(current_interval, key=lambda t: int(t[1]))
except ValueError:
pass # Code was throwing errors for blank entries, this just skips them
# Write the values for the current interval
datawriter.writerow([interval_count, '', minimum[1], minimum[0], '', maximum[1], maximum[0]])
interval_count += 1 # Update count