This is part of a series on data storage with Python.
In this post, we'll be covering plain .txt files. This is the most generic way to store data as it is very easy to edit (using any text editor), but it is also the least structured. Text files are pretty much the electronic version of free-form notes on a paper notepad.
In python, you can write data into a .txt file with the following code:
text = """These are the contents of a multi-line string Everything between the three quotation marks will be written into the file. If you wanted to jot down some numbers, you can do so here. There is no standardized format for this. Voltage (V) | Readout (V) ------------+------------ 10.2 | 3.8 5.6 | 9.2 8.4 | 8.7 """ with open('filename.txt', 'w') as f: f.write(text)
And then read it back like this
with open('filename.txt') as f: data = f.read()
Text files are okay if you need a quick place to dump something electronically, but it is very easy for them to get lost somewhere random in your computer directory. The offer the same freedom as jotting a note down on paper, but it's probably better that you just write it down in a bound notebook instead.