<div dir="ltr">Hi everybody,<div><br></div><div>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:</div>
<div><br></div><div><div>Traceback (most recent call last):</div><div> File "C:\Python31\MyCSVProjectFinal.py", line 83, in <module></div><div> writer.writerow(headings)</div><div>IOError: [Errno 9] Bad file descriptor</div>
</div><div><br></div><div>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):</div><div><br></div><div><div>headings = linesInCSV[0] # e.g. ['Measured1', 'Measured2'] </div>
<div> csvOutFileName = easygui.filesavebox(title = "Choose output file for averages", ) </div><div> if csvOutFileName is not None: </div><div> print "Saving using: "+csvOutFileName </div>
<div> csvOut = file(csvOutFileName, 'rb') </div><div> writer = csv.writer(csvOut) </div><div><b> WRITER.WRITEROW(HEADINGS)</b></div><div> for index in range(len(measured1)): </div><div>
writer.writerow([measured1[index], measured2[index]]) </div><div> writer.writerow([averaged1, averaged2]) </div><div> else: </div><div> print "No filename for saving"</div><div><br>
</div><div><br></div><div>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.</div><div><br></div><div>I hope you can be of help.</div>
<div><br></div><div>PS: I've added the complete code below for reference.</div><div><br></div><div>thanks </div></div><div><br></div><div><br></div><div><div>import csv </div><div>import sys </div><div>import easygui </div>
<div> </div><div>def getFileAndPath(): </div><div> "Get fully-qualified path to the csv file" </div><div> # TODO argInitialFile = '*.csv' </div><div> fileAndPath = easygui.fileopenbox(title="Select .CSV file") </div>
<div> print "Using:",fileAndPath </div><div> return fileAndPath </div><div> </div><div>def getLinesInCSVFile(fileAndPath): </div><div> "read lines in CSV file, return a list of these lines" </div>
<div> linesInCSV = [] </div><div> reader = csv.reader(open(fileAndPath, "rb")) </div><div> for row in reader: </div><div> linesInCSV.append(row) </div><div> return linesInCSV </div><div> </div>
<div>def justNumbers(listOfStrings): </div><div> "True if the list contains just numbers represented as strings" </div><div> # e.g. ['22.4', '23.9'] </div><div> isJustNumbers = True </div>
<div> for item in listOfStrings: </div><div> try: </div><div> nbr = float(item) </div><div> except ValueError: </div><div> isJustNumbers = False </div><div> return isJustNumbers </div>
<div> </div><div>def getNumbers(listOfStrings): </div><div> "Convert a list of strings-of-numbers to a list of numbers, e.g. ['22.4', '23.9'] -> [22.4, 23.9]" </div><div> numbers = [] </div>
<div> for item in listOfStrings: </div><div> nbr = float(item) </div><div> numbers.append(nbr) </div><div> return numbers </div><div> </div><div>def average(values): </div><div> """Computes the arithmetic mean of a list of numbers""" </div>
<div> return sum(values, 0.0) / len(values) </div><div> </div><div>if __name__ == "__main__": </div><div> # get the file-name </div><div> #fileAndPath = getFileAndPath() </div><div> # NOTE quick hack to make our test/development process quicker </div>
<div> fileAndPath = "c:\\testing\\measured2.csv"</div><div> </div><div> # read the CSV file </div><div> linesInCSV = getLinesInCSVFile(fileAndPath) </div><div> </div><div> measured1 = [] </div>
<div> measured2 = [] </div><div> for n in range(1,4): </div><div> line = linesInCSV[n] </div><div> isJustNumbers = justNumbers(line) </div><div> if not isJustNumbers: </div><div> print "ERROR! Expected a line of numbers, instead we got:",line</div>
<div> sys.exit() </div><div> # we only get here if justNumbers reports that we only have numbers </div><div> # so we can extract the list of floating-point numbers </div><div> numbers = getNumbers(line) </div>
<div> measured1.append(numbers[0]) </div><div> measured2.append(numbers[1]) </div><div> averaged1 = average(measured1) </div><div> averaged2 = average(measured2) </div><div> </div><div> # Show values of Measured1 in a choicebox </div>
<div> # We don't care about the choices, this is just for output </div><div> #easygui.choicebox(message = "Sorted values in Measured1", title = "Measured1", choices = measured1) </div><div>
</div><div> headings = linesInCSV[0] # e.g. ['Measured1', 'Measured2'] </div><div> csvOutFileName = easygui.filesavebox(title = "Choose output file for averages", ) </div><div> if csvOutFileName is not None: </div>
<div> print "Saving using: "+csvOutFileName </div><div> csvOut = file(csvOutFileName, 'rb') </div><div> writer = csv.writer(csvOut) </div><div> writer.writerow(headings) </div>
<div> for index in range(len(measured1)): </div><div> writer.writerow([measured1[index], measured2[index]]) </div><div> writer.writerow([averaged1, averaged2]) </div><div> else: </div><div>
print "No filename for saving"</div></div></div>