Numpy ValueError:无法将字符串转换为浮点:电压1
对不起,我才刚开始,我不太明白这个情节是怎么回事:Numpy ValueError:无法将字符串转换为浮点:电压1,numpy,matplotlib,Numpy,Matplotlib,对不起,我才刚开始,我不太明白这个情节是怎么回事: #!/usr/bin/python import matplotlib.pyplot as plt import numpy as np import matplotlib.dates as mdates File = open('file.txt', 'r') readFile = File.read() data = [] split_data = readFile.split('\n') for line in split_dat
#!/usr/bin/python
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.dates as mdates
File = open('file.txt', 'r')
readFile = File.read()
data = []
split_data = readFile.split('\n')
for line in split_data:
split_line = line.split('\t')
if len(split_line) == 4:
if '[V]' not in line:
data.append(line)
voltage, current, abscurrent, time = np.loadtxt(data, delimiter='\t', unpack=True)
plt.plot(voltage, current)
file.txt如下所示:
Starttime: 28.11.2016, 12:01:11
Iterations SMU1: 1
Configuration: from -2.0V to 2.0V in 0.100V-steps, hold: 0.1s, stop: 0.0s, repeated: 1x
Voltage 1 Current 1 absCurrent 1 Time
[V] [A] [A] [s]
-1.99993e+00 -5.35746e-07 5.35746e-07 4.802936e+02
-1.89992e+00 -4.82880e-07 4.82880e-07 4.805996e+02
-1.79997e+00 -4.34462e-07 4.34462e-07 4.809054e+02
-1.69994e+00 -3.89697e-07 3.89697e-07 4.812115e+02
-1.59992e+00 -3.48121e-07 3.48121e-07 4.815175e+02
-1.50000e+00 -3.10000e-07 3.10000e-07 4.818237e+02
然后我总是会遇到这样的错误:
ValueError: could not convert string to float: Voltage 1
如果我删除这条线,图形将正常绘制。。。
谢谢:)可能是因为此行包含四个选项卡:
Voltage 1 Current 1 absCurrent 1 Time
这就是本条件所要求的:
split_line = line.split('\t')
if len(split_line) == 4: # Are there four tabs?
你也许可以用空格来代替制表符?这显然是在试图将该字符串解释为一个浮点数。该行看起来像标题行,但使用
unpack
时,您不需要它
因此,您只需确保数据
列表中不包含它<代码>数据应仅包括带有4个浮点数的行。您已经在筛选出其他标题行方面做了大量工作。完成这项工作
在任何测试或调试期间,您是否打印了数据
loadtxt
拆分分隔符上的数据行,然后将每个字符串转换为所需的数据类型。默认值是float
Out[65]: ['-1.50000e+00', '-3.10000e-07', '3.10000e-07', '4.818237e+02']
In [66]: [float(astr) for astr in line.split()]
Out[66]: [-1.5, -3.1e-07, 3.1e-07, 481.8237]
In [67]: line='Voltage 1 \t Current 1 \t absCurrent 1 \t Time'
In [68]: line.split('\t')
Out[68]: ['Voltage 1 ', ' Current 1 ', ' absCurrent 1 ', ' Time']
In [69]: float(_[0])
...
ValueError: could not convert string to float: 'Voltage 1 '
没有,只有3个。我已经尝试添加另一个限制[和“电压”:]以消除线路,但问题仍然是
'V'不在线路中
应跳过[V]
线路和电压1
线路。或者,如果需要,使用电压跳过线路。