i want to create an array with numpy.
The base is a .txt file which is given in the following form:
i tried it with loadtxt:
data = np.loadtxt("myfile.txt",delimiter='\n',skiprows = 1)
The first row with "Zeit" and "Signal" should be skipped i only need the array in this form:
[[0, 1], [0.01, 2], [0.02, 3]]
The Values from "Zeit" and "Signal are seperated with a tab.
i got this error:
ValueError: could not convert string to float:
解决方案
I mistakenly tried to recreate your file as
Zeit\tSignal
0.01\t1.1
0.02\t1.105
0.03\t1.108
0.04\t1.2
0.05\t1.205
0.06\t1.209
loaded with:
data=np.loadtxt('test.txt', skiprows=1)
and got:
ValueError: could not convert string to float: b'0.01\\t1.1'
Changing the string \t to real tabs (hard to convey this with copy-n-paste):
In [474]: data=np.loadtxt('test.txt', skiprows=1)
In [475]: data
Out[475]:
array([[ 0.01 , 1.1 ],
[ 0.02 , 1.105],
[ 0.03 , 1.108],
[ 0.04 , 1.2 ],
[ 0.05 , 1.205],
[ 0.06 , 1.209]])
Make sure that your file contains just these characters (with spaces or real tabs between columns). It should plain ASCII, not RTF or other formatted test.
It may be worth trying a string dtype just to see if just a few funny chacacters are giving a problem, eg.
In [478]: data=np.loadtxt('test.txt', dtype=str)
In [479]: data
Out[479]:
array([["b'Zeit'", "b'Signal'"],
["b'0.01'", "b'1.1'"],
["b'0.02'", "b'1.105'"],
["b'0.03'", "b'1.108'"],
["b'0.04'", "b'1.2'"],
["b'0.05'", "b'1.205'"],
["b'0.06'", "b'1.209'"]],
dtype='