import csv


def ReadCSVData(filename):
	"""Read the operator and function definition data from a CSV file. All of
	the data to create the C code is stored in a spreadsheet and then saved to
	a CSV file. This function reads in the file, and saves it in a list of
	dictionaries. While doing this, sanatize the data to take out "'" characters
	which were added to prevent conflicts with spreadsheet codes. 

	The first row of the spreadsheet is used as the expected key names.

	"""
	csvreaddata = []
	with open(filename, 'r') as csvfile:
		opreader = csv.reader(csvfile, delimiter='\t')
		# The first row is the descriptive headers, which we use as key names.
		dataformat = next(opreader)
		# Read in all the data at once so we can work on it more easily. 
		for rec in opreader:
			csvreaddata.append(dict([(x,y.replace("'", '')) for x,y in zip(dataformat, rec)]))

		# Sanitise the data to remove quote characters which were added to the
		# spreadsheet to prevent conflicts with spreadsheet formatting codes.
		csvdata = []
		for rec in csvreaddata:
			rec['pyoperator'] = rec['pyoperator'].replace("'", '')
			csvdata.append(rec)


	return csvdata