【发布时间】:2015-06-30 08:03:54
【问题描述】:
我有这段代码用于从 Picotech 的 TC-08 温度记录器读取温度并将温度写入 CSV 文件。问题是温度以四位小数存储在文件中,两位小数甚至一位对我来说已经足够了。
我尝试使用 np.around(temp,2) 和 np.set_printoptions(precision=2) 但它们都不会更改写入 CSV 文件的小数位数。你能帮我告诉我正确的方法吗?
csv_delimiter='\t'
file = 'test.csv'
no_of_channels=6 #set number of channels to read
tc_type=ord('K') #set type of element
no_of_meas = 10
time_interval= 10 #read every 10 sec
#Setup TC08
mydll = ctypes.windll.LoadLibrary('usbtc08.dll')
device = mydll.usb_tc08_open_unit()
mydll.usb_tc08_set_mains(device,50) #set the mains rejection to 50 or 60 Hz
temp = np.zeros( (9,), dtype=np.float32)
overflow_flags = np.zeros( (1,), dtype=np.int16)
mydll.usb_tc08_set_channel(device, 0, 0 )
no_of_channels +=1 #Don't want to read channel 0
for i in range(1,no_of_channels):
mydll.usb_tc08_set_channel(device,i,tc_type)
cur_meas = 1
with open(file, 'a', newline='') as fp:
while cur_meas <= no_of_meas:
timeBegin = time.time()
cur_time = datetime.datetime.strftime(datetime.datetime.now(), '%H:%M:%S')
a = csv.writer(fp, delimiter=csv_delimiter)
mydll.usb_tc08_get_single(device, temp.ctypes.data, overflow_flags.ctypes.data, 0)
#np.around(temp,2)#I have tried this in order to get two decimals
#np.set_printoptions(precision=2)#I have tried this in order to get two decimals
print(temp)
listtemp = temp[1:no_of_channels]
print(listtemp)
data = [[cur_time]+list(listtemp)]
a.writerows(data)
fp.flush()
os .fsync(fp.fileno())
print(', '.join(map(str, data)))
cur_meas += 1
timeEnd = time.time()
timeElapsed = timeEnd - timeBegin
time.sleep(time_interval-timeElapsed)
mydll.usb_tc08_close_unit(device)
【问题讨论】:
-
您能否尝试develop a smaller example 仍然存在问题?