0

I have a csv file named 'datavalues.csv'. It has about 20,000 values and I am curious as to how many of those values are over 10,000.

I have the below code, but I keep getting the following error ValueError: I/O operation on closed file

#!/usr/bin/python

import csv

with open('datavalues.csv', 'rb') as datavalues:
  datavaluesreader = csv.reader(datavalues, delimiter=',')

print(sum(x >= 10000 for x in datavaluesreader))
Praveen Rewar
  • 952
  • 1
  • 7
  • 18
Dallas
  • 3
  • 3
  • 6
    Try moving the `print` inside the `with`. Still, the data may be strings, so you have to convert them to integers. – iz_ Aug 10 '19 at 20:26
  • 1
    `x` is the row. You have to select a cell and convert to integer. e.g `sum(int(x[0]) >= 10000 for x in datavaluesreader)` – Jean-François Fabre Aug 10 '19 at 20:29

1 Answers1

1

Look this ValueError : I/O operation on closed file. I think that is your problem. The usage of datavaluesreader is not inside the scope of the with statement, and so by the time it is executed the underlying file has been closed.

holdenweb
  • 33,305
  • 7
  • 57
  • 77
jke94
  • 11
  • 1