【发布时间】:2018-02-05 04:18:47
【问题描述】:
在对 CSV 文件执行 np.loadtxt() 后,我看到以下基于仅包含 1 行数据的文件的结果。
dcsv: [ 45.29625588 -75.92420601 64.66075832 2.25 ]
dcsv shape: (4L,)
dcsv type: float64
DataSet Length dcsv: 4 Rows
我不知道,为什么它认为有 4 行数据。它识别有 0 行和 2 行或更多,但当只有 1 时返回 4。
2 行示例:
dcsv: [[ 45.29638366 -75.92428038 64.8255732 1. ]
[ 45.29638376 -75.92427927 64.82507261 13. ]]
dcsv shape: (2L, 4L)
dcsv type: float64
DataSet Length dcsv: 2 Rows
0 行示例:
dcsv: []
dcsv shape: (0L,)
dcsv type: float64
DataSet Length dcsv: 0 Rows
dcsv = []
dcsv = np.loadtxt('Start5SecondsSIMPy2JS.csv',delimiter=',',skiprows=0,usecols = (0,1,2,3))
print "dcsv:", dcsv
print "dcsv shape:", dcsv.shape
print "dcsv type: ", dcsv.dtype
print "DataSet Length dcsv:",dcsvLen,"Rows"
提供解决方案将不胜感激。
我需要解决方案来处理 0,1,2.....10,000+ 行。 np.atleast_2d 和 ndim 都产生了 1 的结果,有 0 行和 1 行。我的解决方案是使用 np.size,它返回 0 表示 0 行,返回 4 表示 1 行,并且这些方法中的任何一种都允许我识别具有 0 行或 1 行的文件。 np.ndarray.len 将处理任何数量 >= 2 的行。
【问题讨论】:
-
如果您希望形状为 (1, 4),请使用参数
ndmin=2。
标签: python arrays csv numpy dimensions