[英]reading scientific notation csv file with numpy
I'm trying to load a file that represents a 2D matrix with numpy loadtxt 我正在尝试加载一个代表带有numpy loadtxt的2D矩阵的文件
cov = np.loadtxt("cov.csv")
The first element is in scientific notation which causes a failure 第一个要素是科学记数法导致失败
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-80-0796abd8c0f7> in <module>()
----> 1 cov = np.loadtxt("cov.csv")
C:\home\Anaconda3\lib\site-packages\numpy\lib\npyio.py in loadtxt(fname, dtype, comments, delimiter, converters, skiprows, usecols, unpack, ndmin)
858
859 # Convert each value according to its column and store
--> 860 items = [conv(val) for (conv, val) in zip(converters, vals)]
861 # Then pack it according to the dtype's nesting
862 items = pack_items(items, packing)
C:\home\Anaconda3\lib\site-packages\numpy\lib\npyio.py in <listcomp>(.0)
858
859 # Convert each value according to its column and store
--> 860 items = [conv(val) for (conv, val) in zip(converters, vals)]
861 # Then pack it according to the dtype's nesting
862 items = pack_items(items, packing)
ValueError: could not convert string to float: b'5.0e-7,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0'
I couldn't find any documentation on how to make this work. 我找不到任何关于如何使这项工作的文件。
numpy version: 1.9.2
python version: 3.4.3 |Anaconda 2.3.0 (32-bit)| (default, Mar 6 2015, 12:08:17) [MSC v.1600 32 bit (Intel)]
It's not scientific notation that's the problem; 这不是科学记数法,而是问题; it's that the
loadtxt
default delimiter is any whitespace. 这是
loadtxt
默认分隔符是任何空格。 You're reading a csv, so specify delimiter=","
: 你正在读csv,所以指定
delimiter=","
:
>>> np.loadtxt("cov.csv")
Traceback (most recent call last):
File "<ipython-input-1-6fdfa7467ef5>", line 1, in <module>
np.loadtxt("cov.csv")
File "/usr/local/lib/python3.4/dist-packages/numpy/lib/npyio.py", line 928, in loadtxt
items = [conv(val) for (conv, val) in zip(converters, vals)]
File "/usr/local/lib/python3.4/dist-packages/numpy/lib/npyio.py", line 928, in <listcomp>
items = [conv(val) for (conv, val) in zip(converters, vals)]
File "/usr/local/lib/python3.4/dist-packages/numpy/lib/npyio.py", line 659, in floatconv
return float(x)
ValueError: could not convert string to float: b'5.0e-7,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0'
>>> np.loadtxt("cov.csv", delimiter=",")
array([ 5.00000000e-07, 0.00000000e+00, 0.00000000e+00,
0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
0.00000000e+00, 0.00000000e+00, 0.00000000e+00])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.