I have a csv file of the following format:
x1 y1 z1 x2 y2 z2 cost
1 2 3 4 4 5 60
...etc in the excel. However, in wordpad, it will be represented like so:
x1,y1,z1,x2,y2,z2,cost
1,2,3,4,4,5,60
Basically, where the delimiter is ','. I am trying to read this csv file using numpy, specifically using genfromtxt. Here is my code:
import numpy as np
import csv
from StringIO import StringIO
with open ('1250_12.csv','rb') as csvfile:
data = np.genfromtxt(csvfile, dtype = None, delimiter = ',')
print data
Although there is no errors, my program prints out
[['x1' 'y1' 'z1' ..., 'y2' 'z2' 'cost']
['5720.44' '3070.94' '2642.19' ..., '3061.01' '2576.29' '102.12']
['5720.44' '3070.94' '2642.19' ..., '3023.6' '2597.81' '110.4']
...,
['5748.56' '3102' '2631.75' ..., '3215.74' '2657.41' '148.58']
['5748.56' '3102' '2631.75' ..., '3156.07' '2598.65' '110.08']
['5748.56' '3102' '2631.75' ..., '3178.16' '2627.18' '132.85']]
I know the numbers are different but lets pretend they are the same. Basically, this program prints out the first 3 and last 3 rows of my csv file. This may be due to the csv file being too large, i'm not sure. Another problem is that 'x1' and all its data is vanished. My question is, is this even an error? Did everything vanish because the file was to big? I am really new to numpy
np.genfromtxt('1250_12.csv', dtype = None, delimiter = ',', skipheader=1)
if you add the skipheader argument, you can skip the line with x1, y1 in it, and you will get an array of numbers, where you currently have a list of lists of strings. You can also pass the filename directly, removing one line from your code. – gggg Jul 31 '13 at 16:43print data[0,:]
andprint data[:,0]
andprint data.shape
to learn a bit more about the shape of the resulting data object. – gggg Jul 31 '13 at 16:49