简体   繁体   English

写入CSV文件的数据中的小数位数格式

[英]Format number of decimals in data written to CSV file

I have this piece of code for reading temperature from TC-08 temperaturelogger from Picotech and writing the temperature to a CSV file. 我有这段代码可以从Picotech的TC-08温度记录仪读取温度并将温度写入CSV文件。 The problem is that the temperatures are stored with four decimals in the file and two decimals or even one is good enough for me. 问题是温度在文件中存储的是四位小数,而两位小数或什至一位对我来说已经足够了。

I have tried to use np.around(temp,2) and np.set_printoptions(precision=2) but none of them changes the number of decimals written to the CSV-file. 我尝试使用np.around(temp,2)和np.set_printoptions(precision = 2),但是它们都没有改变写入CSV文件的小数位数。 Could you help me out and tell me the proper way to do it? 您能帮我一下,告诉我正确的方法吗?

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)

Stating the obvious, the csv is text, so the precision displayed when imported may be different for each cell. 显而易见,csv是文本,因此导入时显示的精度对于每个单元格可能会有所不同。 the round function does not guarantee two decimal precision, however formatting a float does. round函数不能保证两位十进制精度,但是格式化浮点数可以。

f1 = 100.0001
f2 = 99.2345
f3 = 88.7455
f4 = 1.4589
lf1 = [f1, f2, f3, f4]

for f in lf1:
    print f
    print str(round(f, 2))
    print '{0:0.2f}'.format(f) # rounds and formats to two decimal precision

100.0001
100.0
100.00
99.2345
99.23
99.23
88.7455
88.75
88.75
1.4589
1.46
1.46

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM