Hi everybody, I'm running python 2.6.1 on vista and I'm trying to use the csv module to write to a csv file and get the average of some numbers, but I keep getting the following error:
Traceback (most recent call last): File "C:\Python31\MyCSVProjectFinal.py", line 83, in <module> writer.writerow(headings) IOError: [Errno 9] Bad file descriptor line 83 refers to the following code, specifically to the one in capital (in the actual code it's not in capital by the way): headings = linesInCSV[0] # e.g. ['Measured1', 'Measured2'] csvOutFileName = easygui.filesavebox(title = "Choose output file for averages", ) if csvOutFileName is not None: print "Saving using: "+csvOutFileName csvOut = file(csvOutFileName, 'rb') writer = csv.writer(csvOut) * WRITER.WRITEROW(HEADINGS)* for index in range(len(measured1)): writer.writerow([measured1[index], measured2[index]]) writer.writerow([averaged1, averaged2]) else: print "No filename for saving" so, my problem is I don't know why it keeps giving me this error. I've checked on the internet, but I haven't found anything to help resolve this error. I hope you can be of help. PS: I've added the complete code below for reference. thanks import csv import sys import easygui def getFileAndPath(): "Get fully-qualified path to the csv file" # TODO argInitialFile = '*.csv' fileAndPath = easygui.fileopenbox(title="Select .CSV file") print "Using:",fileAndPath return fileAndPath def getLinesInCSVFile(fileAndPath): "read lines in CSV file, return a list of these lines" linesInCSV = [] reader = csv.reader(open(fileAndPath, "rb")) for row in reader: linesInCSV.append(row) return linesInCSV def justNumbers(listOfStrings): "True if the list contains just numbers represented as strings" # e.g. ['22.4', '23.9'] isJustNumbers = True for item in listOfStrings: try: nbr = float(item) except ValueError: isJustNumbers = False return isJustNumbers def getNumbers(listOfStrings): "Convert a list of strings-of-numbers to a list of numbers, e.g. ['22.4', '23.9'] -> [22.4, 23.9]" numbers = [] for item in listOfStrings: nbr = float(item) numbers.append(nbr) return numbers def average(values): """Computes the arithmetic mean of a list of numbers""" return sum(values, 0.0) / len(values) if __name__ == "__main__": # get the file-name #fileAndPath = getFileAndPath() # NOTE quick hack to make our test/development process quicker fileAndPath = "c:\\testing\\measured2.csv" # read the CSV file linesInCSV = getLinesInCSVFile(fileAndPath) measured1 = [] measured2 = [] for n in range(1,4): line = linesInCSV[n] isJustNumbers = justNumbers(line) if not isJustNumbers: print "ERROR! Expected a line of numbers, instead we got:",line sys.exit() # we only get here if justNumbers reports that we only have numbers # so we can extract the list of floating-point numbers numbers = getNumbers(line) measured1.append(numbers[0]) measured2.append(numbers[1]) averaged1 = average(measured1) averaged2 = average(measured2) # Show values of Measured1 in a choicebox # We don't care about the choices, this is just for output #easygui.choicebox(message = "Sorted values in Measured1", title = "Measured1", choices = measured1) headings = linesInCSV[0] # e.g. ['Measured1', 'Measured2'] csvOutFileName = easygui.filesavebox(title = "Choose output file for averages", ) if csvOutFileName is not None: print "Saving using: "+csvOutFileName csvOut = file(csvOutFileName, 'rb') writer = csv.writer(csvOut) writer.writerow(headings) for index in range(len(measured1)): writer.writerow([measured1[index], measured2[index]]) writer.writerow([averaged1, averaged2]) else: print "No filename for saving"
_______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor