So in many physics programs you to very demanding calculations and then only afterwards you think about
what do you want to do with the data?
Which type of processing do you want to do?
So of course, it would be very unreasonable to redo the whole calculation every time.
So at some point you want to store your data so that you can load the data later on without having to
recalculate it.
And we want to do this here with our store list from the previous section.
So I want to show you, how can you save these numbers in a file?
And I want to show you several methods how this works.
So the first thing is that I want to show you how you can save the whole thing at once.
So the component here is that it relies on Nampai, so it's a can and p command and p dot and it's called
safe text or safe text.
And then the syntax is the following you just write store lists.
So basically you give the string for for you.
A file that will be created stored is one dot.
Yet, for example, you could also name a text or some something you like.
It doesn't really matter, but typically you call it something like that.
And then you write the variable that you want to plot or that you want to export.
So in our case, that's what that will be used to store list.
And then something that I found really useful is to add another option, which is formats and the format.
You write something like this.
So what it means, I will show you in a second.
So we run this and it will create a new file in the same directory where your notebook is stored.
So now let me show you what happens if I do the same thing without the format option.
So let's call it stolen two and run this as well.
And now I will show you what happens.
So here you see, the output of all is one with the optimal form and commands.
So this will just be our integer numbers.
And here you see the output of Stal is two, which will be your floating point numbers even with this
10 to the power of one here.
So I think this is not really so useful.
But of course, if you are using floating point numbers, then it's of course better to have such a
formant.
But even the optimal tag empty percentage as also works well with floating point numbers.
So I recommend always using this format so that it will always give you a reasonable output.
So now let's get back to our Jupyter notebook, and let's close these two facts of what we have done
here is we have taken a finished list and we have saved the whole list at once in the new file.
But sometimes, especially if it's a very, very long calculation and you want to check in between what
is the results, or even if you are afraid that your calculation may crash and may stop at some point
and then you lose all the data because this one hasn't finished and you couldn't export it.
So in these cases, it's always better to save one line after another.
And you can do this in several methods, of course, and I want to show you here two of these.
So the first one would be to, first of all, open a file.
So, for example, we could write store list three point eighty and then we provide another argument,
which is W-4, right?
So this allows us to write in this file.
And also it means that when this file doesn't exist, it will create the file.
So now, in order to work with this file, we must store this command in a variable, or we must give
it a name, basically.
So I call this file and now we can access the file by writing something like file dot, right?
So we can write file dog right.
And we can now write some, yes, some stuff, for example.
Hello.
So this is especially useful if you're working with loops, as we have learned in the previous section.
So here we'll use a for loop, so I will write.
Let me maybe just copy this one here.
So let me redo the calculation from here.
So I write for I in range one, two, six, and no, I don't want to store the data in a list, but
instead I want to write.
This to the files I write file DOD, right?
So I could now try to run it like this, but I think it will not work so well, let's see.
Yeah.
So the error will be that the right argument must be a string and not an integer.
So what we can do quite easily, we just convert this number to a string.
I think this was wrong.
It's not correct.
Of course it's string.
So now I think it has worked.
So let me check what the file looks like.
So, yeah, it worked.
But it didn't really give us the result that we wanted.
It basically just printed all of the strings, just one after another and not in a new line.
So, yeah, that's a bit problematic.
And also, there's another thing that I want to show you.
So when I tried to open the file after running the command and I just wrote down, it gave me an error
message that I couldn't open the file because it was currently being used in the Python notebook.
So let me show you what I changed.
But the problem was that the file was still open because I just wrote open the file and then we wrote,
but we never closed it.
So this was why I got the error message that I couldn't access to file.
That's what I had to add here is I had to close the file.
But of course, be careful and do not write this into for a loop.
So do not write it here.
This would be pretty bad because then you would close it right after the first loop and then you can't
write in the file anymore.
And then to solve our other problem that we have all of the characters just one after another.
We just have to write a line break.
And this is just like in HTML.
I think you just write the following commands backslash.
And so this is the command for a line break.
So I think if I run this now, it should work.
So here we go.
We have now our five numbers.
Just one number in every line.
So now let me go back to the to the course and let me show you another method how you can save this
data in the file.
And this method is just a bit shorter.
So here we use something called the width command.
So we write with.
Open.
And then we make a new file, which we call Story List four, and we open this as a file and then we
can write here our commands.
So basically, this means just in this blog, just for these commands, we have to file open and we
call the file file.
So I could write you also something else, for example, test, then our file would be called test.
So now just in this block, we can write in the file.
And now what I do is I just copy this one and paste it here, and I think this should also work.
And here we don't even have to close the file because it means just in the block, we have to file open.
And that means while the block is running or while the loop is running, it's open but wants to loop
has finished and wants to.
Whole block has finished.
Then the file will be closed automatically.
So let me run it and see what happens.
So once again, it worked.
We have our five numbers one, four, nine, 16 and 25.
So we have now learned how to store data and files by basically three different methods.
And I would say this one is most easy, but these ones are sometimes a bit more useful because, yeah,
you always write in the file while you calculate and it's much safer.
And also, yeah, you can check while you're calculating what is the result.
Now, of course, we do not only want to write in the file, we also want to load.
And for this, I will use the command and p note text, although to text.
Now we must specify which file we want to load, and I just will load the data from one of our files
and we have just created which is in the same directory so we can just write this and we must, of course,
assign a name to it.
So this will be our loaded list.
And now I can check if this really worked.
So I write node list and it gives me the array one, four, nine, 16 and twenty five.
And of course, I could now continue and work with the data that we have just loaded.
For example, just write a lot of lists zero and it will give me the first entry, which is one dot
zero.
So you see, loading data is pretty easy.
If the data stored in the correct format, so I would always encourage you to use one of these methods
so that you have your data one number in every line.
So then there will be no problem when you load the data.