我成功地简化了从光谱仪导入数据的python模块 (我完全是个初学者,别人为我写了代码的模型……)
我只有一个问题:一半的输出数据(在.csv
文件中)被方括号包围:[]
我希望文件包含如下结构:
name, wavelength, measurement
即
a,400,0.34
a,410,0.65
...
但我得到的是:
a,400,[0.34]
a,410,[0.65]
...
有什么简单的解决办法吗?你知道吗
是因为measurement
是string
?你知道吗
谢谢
import serial # requires pyserial library
ser = serial.Serial(0)
ofile = file( 'spectral_data.csv', 'ab')
while True:
name = raw_input("Pigment name [Q to finish]: ")
if name == "Q":
print "bye bye!"
ofile.close()
break
first = True
while True:
line = ser.readline()
if first:
print " Data incoming..."
first = False
split = line.split()
if 10 <= len(split):
try:
wavelength = int(split[0])
measurement = [float(split[i]) for i in [6]]
ofile.write(str(name) + "," + str(wavelength) + "," + str(measurement) + '\n')
except ValueError:
pass # handles the table heading
if line[:3] == "110":
break
print " Data gathered."
ofile.write('\n')
请执行以下操作:
或
相关问题 更多 >
编程相关推荐