简体   繁体   English

在python 2.7中使用numpy读取CSV文件

[英]Reading CSV file using numpy in python 2.7

I have a csv file of the following format: 我有以下格式的csv文件:

x1    y1    z1    x2    y2    z2    cost
 1     2     3     4     4     5      60

...etc in the excel. ...等在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. 我正在尝试使用numpy读取此csv文件,特别是使用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. 基本上,该程序打印出我的csv文件的前3行和后3行。 This may be due to the csv file being too large, i'm not sure. 这可能是由于csv文件太大,我不确定。 Another problem is that 'x1' and all its data is vanished. 另一个问题是“ x1”及其所有数据都消失了。 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 在打印数据之前

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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