Write & Read Floating Point Data To File
After creating my own file (“w”), I would like to know how to:
- Write floating point data to file
- Print (or otherwise view) entire contents of file to confirm it contains all the float data I wrote to the file
After re-opening my file (“r”), I would like to know how to:
- Read content of this file, line by line.
- Print (or otherwise view) contents of file to re-confirm it contains all the float data I wrote to it
- Use the float data contained in my file for doing math calculations
- Save calculation results (floats) to another file
- Print (or otherwise view) contents of this other file to re-confirm it contains all the calculation results
There are a few ways, depending on what you are trying to do.
If you want to read tabular type data,
csvis a fine option. For saving python variables,
pickleare good, and
yamlalso read into dicts, and produce readable files. If you literally just want to store one double per line without any context, you could simply call float. Or, if you are using
formatto write each line, use
parseas the inverse of format.
For just reading/writing your own format, you can use
for line in f: my_num=float(line) #do something with my_num...
Thx. I think my problem boils down to a personal lack of rudimentary understandithe form that my floating point data must be converted to before Python will allow it to be written to my file, via write().
I think I’m suppose to convert each floating point data item to a representative text string via str(), before writing to my file via write(). I understand that, by doing this conversion, my file will contain ASCII strings that are the text equivalents of my original raw floating point data. I believe that, if I fail to do this conversion prior to Python will likely not save my data to my file as floating point numbers.
I would much appreciate any feedback as to whether or not my interpretation is on track.
Thank you … Phlurim
import json with open("floats.txt", "w") as out_file: json.dump([1.11 * i for i in range(10)], out_file) with open("floats.txt") as in_file: floats = json.load(in_file)
[0.0, 1.11, 2.22, 3.33, 4.44, 5.550000000000001, 6.66, 7.7700000000000005, 8.88, 9.99]
Thx for the code snippet. I see what’s happening here w.r.t. json.dump & json.load, but still not clear on the format of the data that ends up in ‘floats.txt’, ‘out_file’ , and ‘in_file’ after each of these json methods. I’m guessing ‘floats.txt’ always contains string formatted data only. But what’s in ‘out_file’ , and ‘in_file’! In which case, how then can I read it - retrieve it from floats.txt - and use it as float variables instead of string variables? (This probably gives a good idea just where I am in learning Python - I’ve a looong way to go!)
Using the Pythonista editor, open the file
floats.txtto see exactly how the floats are stored.
This table might help.
how then can I read it - retrieve it from floats.txt - and use it as float variables instead of string variables?
The second part of the above code (json.load) reads the file into an array of float variables, not as string variables
But what’s in ‘out_file’ , and ‘in_file’!
Both are file objects, not physical files. Only floats.txt is the physical file