简体   繁体   中英

Reading CSV file using numpy in python 2.7

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

Try putting:

np.set_printoptions(threshold='nan')

before print data

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM