0

I have a .csv file with a lot of data in it. I tried to open it with:

import csv

filename = raw_input('Your filename: ')
with open(filename,'r') as myFile:
  dataFromFile = csv.reader(myFile)
  print dataFromFile

As far as I know csv.reader converts the data from the file into a list.

I wanted to open the file 2015-09-02_17:59:43.csv But I get the following error:

IOError: [Errno 2] No such file or directory: '2015-09-02_17:59:43.csv '

Ok, I googled it and found the following question on stack overflow: Trying to use open( filename, 'w' ) gives IOError: [Errno 2] No such file or directory: So I tried the solution from there. But then I got another error:

OSError: [Errno 2] No such file or directory: ''

Then I renamed my file to timestamp.csv because I was curious if the name might be an issus and tried my first solution. So suddenly the file could be found but as a print output I got the following: <_csv.reader object at 0x104c88a60>. I thought if I printed dataFromFile out it would show me the entire list.

So first question here: Does this <_csv.reader object at 0x104c88a60>mean that the list is to long to be displayed so it just tells me what object I have ?

Second question: Why didn't my previous attempts to solve work?

And I found a little workaround with:

for currentRow in dataFromFile:
 print currentRow

So row after row is printed out. But not exactly what I wanted.

Last but not least I want to transform my list into a numpy array what would be the easiest way ?

EDIT I already found an answer and way for my last question, it is pretty easy I guess:

 import numpy as np
 np.array(dataFromFile)
Community
  • 1
  • 1
Peter
  • 341
  • 1
  • 4
  • 16

2 Answers2

2

As far as I know csv.reader converts the data from the file into a list.

This is the mistaken assumption.

From the Python docs here, csv.reader:

Return a reader object which will iterate over lines in the given csvfile.

It is an iterator, not a list. Thus, you get a csv.reader object. If you did

list(dataFromFile)

it would display what you're expecting.

EDIT: Addressing your edit, I think you're looking for something like from this post. Essentially, genfromtxt() in numpy does what you're trying to do with converting a CSV to numpy array.

P.S. In general, you should probably ask another question if the topic is completely different--keeps it cleaner and makes your question less of a moving target

Community
  • 1
  • 1
James Wang
  • 1,281
  • 9
  • 17
1

the csv.reader function doesn't return a list, it returns an iterator object wich you can use to get line by line. If you want to get a list of lines just do it like this:

#get list of lines
lines = open("your file").readlines()
#if you want to get the list with the values of each line separated by ',' you can do it like this:
lines2 = [line.split(',') for line in open("your file").readlines()]
Ricardo B.
  • 110
  • 1
  • 11