#!/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
如果删除此行,则图表会正常绘制... 谢谢:))
答案 0 :(得分:0)
可能是因为这一行包含四个标签:
Voltage 1 Current 1 absCurrent 1 Time
这就是这种情况所寻求的:
split_line = line.split('\t')
if len(split_line) == 4: # Are there four tabs?
你可以用空格替换标签吗?
答案 1 :(得分:0)
它显然试图将该字符串解释为浮点数。该行看起来像标题行,但unpack
你不需要它。
所以你只需要确保data
列表不包含它。 data
应仅包含4个浮点数的行。您已经在过滤掉其他标题行方面付出了很多努力。完成这项工作。
在任何测试或调试过程中,您是否打印过data
?
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 '