C2 W4 get data "with"

Hi I am trying to start the 4th weeks exercise on the convolutions course. The opening action is to use the csv to return a list of images and a list of labels.
However, the csv is opened and manipulated using a “with” statement. I am used to R but not python and am unfamiliar with this operation or why it is used to open and manipulate a csv. I have been trying to use the function

np.genfromtxt(training_file, delimiter=',', skip_header = 1).astype("int")

to load the data but everytime I run the code chunk it crashes. The “with” approach wasn’t used in the class code so I am not sure what is going on.

Hi @jonno,
through with open, you don’t need to close the file at the end, everything happens inside your nested code and that’s all. The code appears easier to be read, maybe. No specific advantages in CPU time.

Best

Thank you, although this doesn’t really anaswer the question of why the csv just can’t be loaded into memory using a simpler approach. Perhaps this is more obvious for python programmers? Either way in the end I used a for loop because apparently the csv is loaded line by line. I think my overall point is that this function is very different from the other activities and there isn’t much explanation. I spent more time on this one function than the rest of the test combined. Granted, this says a lot about my Python skill!

Give me the benefit of the doubt with this, cause I wasn’t programming python back then, but the python programming language was created in 1991, and pandas (for instance) in 2008. So maybe that was the original way to load csv files. Again, not sure about it. Just trying to guess.

Best

1 Like