i have biological floats and want to save them in a Text file, and then load and make average them. This Floats about :
0.12
0.23
0.30
0.21
..
..
..
This will be saved in Text file.
Average of Floats will be showed in Label.
i have biological floats and want to save them in a Text file, and then load and make average them. This Floats about :
0.12
0.23
0.30
0.21
..
..
..
This will be saved in Text file.
Average of Floats will be showed in Label.
In order to save the floats to a text file, you need to convert them to a string. We convert the list of floats to a list of strings, then join them with a space symbol, which will be the separator and then save the file. In order to read a text file and make that a new list of floats we need to do the same operations but reversed.
About the label, I don't know what GUI framework you are using.
code:
list_of_floats=[0.12, 0.23, 0.30, 0.21]
def save(path,l):
with open(path,'w') as file:
file.write(' '.join(map(str,l)))
def load(path):
with open(path,'r') as file:
return list(map(float,file.read().split()))
save('file.txt',list_of_floats)
new_list=load('file.txt')
print(sum(new_list)/len(new_list))
this is a robust code:
import numpy as np
import os
x = np.arange(12).reshape(4, 3)
x=x/23
print("Original array:")
print(x)
header = 'col1 col2 col3'
np.savetxt('temp.txt', x, fmt="%1.3f", header=header)
print("After loading, content of the text file:")
result = np.loadtxt('temp.txt')
print(result)
print('Lets do a calculation on it :')
print(sum(result)/len(result))