1
00:00:00,270 --> 00:00:08,370
So in many physics programs you to very demanding calculations and then only afterwards you think about

2
00:00:08,370 --> 00:00:09,930
what do you want to do with the data?

3
00:00:10,140 --> 00:00:12,210
Which type of processing do you want to do?

4
00:00:12,900 --> 00:00:18,600
So of course, it would be very unreasonable to redo the whole calculation every time.

5
00:00:19,140 --> 00:00:25,530
So at some point you want to store your data so that you can load the data later on without having to

6
00:00:25,530 --> 00:00:26,760
recalculate it.

7
00:00:27,450 --> 00:00:32,549
And we want to do this here with our store list from the previous section.

8
00:00:33,480 --> 00:00:38,160
So I want to show you, how can you save these numbers in a file?

9
00:00:38,160 --> 00:00:40,650
And I want to show you several methods how this works.

10
00:00:41,790 --> 00:00:49,170
So the first thing is that I want to show you how you can save the whole thing at once.

11
00:00:49,980 --> 00:00:58,890
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

12
00:00:59,160 --> 00:01:01,420
safe text or safe text.

13
00:01:02,280 --> 00:01:07,770
And then the syntax is the following you just write store lists.

14
00:01:09,120 --> 00:01:11,430
So basically you give the string for for you.

15
00:01:11,430 --> 00:01:14,040
A file that will be created stored is one dot.

16
00:01:14,260 --> 00:01:19,590
Yet, for example, you could also name a text or some something you like.

17
00:01:19,590 --> 00:01:23,100
It doesn't really matter, but typically you call it something like that.

18
00:01:24,210 --> 00:01:29,880
And then you write the variable that you want to plot or that you want to export.

19
00:01:30,330 --> 00:01:33,270
So in our case, that's what that will be used to store list.

20
00:01:34,320 --> 00:01:44,430
And then something that I found really useful is to add another option, which is formats and the format.

21
00:01:45,360 --> 00:01:46,710
You write something like this.

22
00:01:47,670 --> 00:01:51,120
So what it means, I will show you in a second.

23
00:01:52,080 --> 00:01:59,340
So we run this and it will create a new file in the same directory where your notebook is stored.

24
00:02:00,810 --> 00:02:06,060
So now let me show you what happens if I do the same thing without the format option.

25
00:02:06,960 --> 00:02:11,340
So let's call it stolen two and run this as well.

26
00:02:11,650 --> 00:02:13,770
And now I will show you what happens.

27
00:02:14,460 --> 00:02:19,920
So here you see, the output of all is one with the optimal form and commands.

28
00:02:20,160 --> 00:02:22,470
So this will just be our integer numbers.

29
00:02:23,340 --> 00:02:30,750
And here you see the output of Stal is two, which will be your floating point numbers even with this

30
00:02:31,560 --> 00:02:33,290
10 to the power of one here.

31
00:02:33,300 --> 00:02:35,970
So I think this is not really so useful.

32
00:02:36,240 --> 00:02:41,940
But of course, if you are using floating point numbers, then it's of course better to have such a

33
00:02:41,940 --> 00:02:42,390
formant.

34
00:02:42,690 --> 00:02:52,950
But even the optimal tag empty percentage as also works well with floating point numbers.

35
00:02:53,370 --> 00:02:59,220
So I recommend always using this format so that it will always give you a reasonable output.

36
00:03:00,300 --> 00:03:05,160
So now let's get back to our Jupyter notebook, and let's close these two facts of what we have done

37
00:03:05,160 --> 00:03:12,510
here is we have taken a finished list and we have saved the whole list at once in the new file.

38
00:03:13,410 --> 00:03:19,530
But sometimes, especially if it's a very, very long calculation and you want to check in between what

39
00:03:19,530 --> 00:03:25,770
is the results, or even if you are afraid that your calculation may crash and may stop at some point

40
00:03:25,770 --> 00:03:31,110
and then you lose all the data because this one hasn't finished and you couldn't export it.

41
00:03:31,800 --> 00:03:36,030
So in these cases, it's always better to save one line after another.

42
00:03:37,110 --> 00:03:41,760
And you can do this in several methods, of course, and I want to show you here two of these.

43
00:03:42,270 --> 00:03:45,870
So the first one would be to, first of all, open a file.

44
00:03:47,160 --> 00:03:56,160
So, for example, we could write store list three point eighty and then we provide another argument,

45
00:03:56,160 --> 00:03:57,630
which is W-4, right?

46
00:03:57,900 --> 00:03:59,880
So this allows us to write in this file.

47
00:04:00,120 --> 00:04:04,380
And also it means that when this file doesn't exist, it will create the file.

48
00:04:05,400 --> 00:04:12,690
So now, in order to work with this file, we must store this command in a variable, or we must give

49
00:04:12,690 --> 00:04:13,650
it a name, basically.

50
00:04:14,160 --> 00:04:21,839
So I call this file and now we can access the file by writing something like file dot, right?

51
00:04:23,250 --> 00:04:25,590
So we can write file dog right.

52
00:04:25,590 --> 00:04:30,170
And we can now write some, yes, some stuff, for example.

53
00:04:30,180 --> 00:04:30,480
Hello.

54
00:04:32,130 --> 00:04:38,190
So this is especially useful if you're working with loops, as we have learned in the previous section.

55
00:04:39,030 --> 00:04:41,640
So here we'll use a for loop, so I will write.

56
00:04:42,510 --> 00:04:45,240
Let me maybe just copy this one here.

57
00:04:45,480 --> 00:04:49,500
So let me redo the calculation from here.

58
00:04:49,500 --> 00:04:57,240
So I write for I in range one, two, six, and no, I don't want to store the data in a list, but

59
00:04:57,240 --> 00:04:59,430
instead I want to write.

60
00:05:00,620 --> 00:05:03,770
This to the files I write file DOD, right?

61
00:05:05,560 --> 00:05:13,450
So I could now try to run it like this, but I think it will not work so well, let's see.

62
00:05:14,470 --> 00:05:14,800
Yeah.

63
00:05:14,980 --> 00:05:19,570
So the error will be that the right argument must be a string and not an integer.

64
00:05:20,350 --> 00:05:25,480
So what we can do quite easily, we just convert this number to a string.

65
00:05:25,900 --> 00:05:26,920
I think this was wrong.

66
00:05:27,160 --> 00:05:27,790
It's not correct.

67
00:05:28,000 --> 00:05:29,080
Of course it's string.

68
00:05:30,400 --> 00:05:31,910
So now I think it has worked.

69
00:05:31,930 --> 00:05:34,010
So let me check what the file looks like.

70
00:05:34,660 --> 00:05:36,640
So, yeah, it worked.

71
00:05:36,640 --> 00:05:39,180
But it didn't really give us the result that we wanted.

72
00:05:39,180 --> 00:05:45,070
It basically just printed all of the strings, just one after another and not in a new line.

73
00:05:46,060 --> 00:05:47,890
So, yeah, that's a bit problematic.

74
00:05:48,610 --> 00:05:50,890
And also, there's another thing that I want to show you.

75
00:05:51,220 --> 00:05:56,890
So when I tried to open the file after running the command and I just wrote down, it gave me an error

76
00:05:56,890 --> 00:06:02,830
message that I couldn't open the file because it was currently being used in the Python notebook.

77
00:06:03,280 --> 00:06:04,750
So let me show you what I changed.

78
00:06:05,080 --> 00:06:12,370
But the problem was that the file was still open because I just wrote open the file and then we wrote,

79
00:06:12,580 --> 00:06:13,680
but we never closed it.

80
00:06:13,720 --> 00:06:17,620
So this was why I got the error message that I couldn't access to file.

81
00:06:18,150 --> 00:06:20,920
That's what I had to add here is I had to close the file.

82
00:06:21,520 --> 00:06:25,060
But of course, be careful and do not write this into for a loop.

83
00:06:25,060 --> 00:06:26,470
So do not write it here.

84
00:06:26,950 --> 00:06:33,070
This would be pretty bad because then you would close it right after the first loop and then you can't

85
00:06:33,070 --> 00:06:34,210
write in the file anymore.

86
00:06:35,470 --> 00:06:41,560
And then to solve our other problem that we have all of the characters just one after another.

87
00:06:41,920 --> 00:06:45,670
We just have to write a line break.

88
00:06:46,480 --> 00:06:49,570
And this is just like in HTML.

89
00:06:49,570 --> 00:06:52,870
I think you just write the following commands backslash.

90
00:06:53,050 --> 00:06:56,590
And so this is the command for a line break.

91
00:06:57,400 --> 00:07:00,160
So I think if I run this now, it should work.

92
00:07:00,640 --> 00:07:01,420
So here we go.

93
00:07:01,450 --> 00:07:03,700
We have now our five numbers.

94
00:07:04,120 --> 00:07:06,460
Just one number in every line.

95
00:07:07,420 --> 00:07:15,790
So now let me go back to the to the course and let me show you another method how you can save this

96
00:07:15,790 --> 00:07:16,810
data in the file.

97
00:07:17,260 --> 00:07:20,140
And this method is just a bit shorter.

98
00:07:21,100 --> 00:07:24,550
So here we use something called the width command.

99
00:07:25,750 --> 00:07:27,010
So we write with.

100
00:07:28,670 --> 00:07:29,250
Open.

101
00:07:30,110 --> 00:07:40,820
And then we make a new file, which we call Story List four, and we open this as a file and then we

102
00:07:40,820 --> 00:07:42,440
can write here our commands.

103
00:07:43,040 --> 00:07:49,220
So basically, this means just in this blog, just for these commands, we have to file open and we

104
00:07:49,220 --> 00:07:51,110
call the file file.

105
00:07:51,740 --> 00:07:57,290
So I could write you also something else, for example, test, then our file would be called test.

106
00:07:58,100 --> 00:08:00,740
So now just in this block, we can write in the file.

107
00:08:01,490 --> 00:08:09,680
And now what I do is I just copy this one and paste it here, and I think this should also work.

108
00:08:10,280 --> 00:08:17,390
And here we don't even have to close the file because it means just in the block, we have to file open.

109
00:08:17,720 --> 00:08:23,480
And that means while the block is running or while the loop is running, it's open but wants to loop

110
00:08:23,480 --> 00:08:24,470
has finished and wants to.

111
00:08:24,470 --> 00:08:25,460
Whole block has finished.

112
00:08:25,730 --> 00:08:28,160
Then the file will be closed automatically.

113
00:08:28,880 --> 00:08:31,040
So let me run it and see what happens.

114
00:08:32,090 --> 00:08:33,340
So once again, it worked.

115
00:08:33,350 --> 00:08:37,340
We have our five numbers one, four, nine, 16 and 25.

116
00:08:38,570 --> 00:08:45,020
So we have now learned how to store data and files by basically three different methods.

117
00:08:45,500 --> 00:08:52,450
And I would say this one is most easy, but these ones are sometimes a bit more useful because, yeah,

118
00:08:52,640 --> 00:08:57,410
you always write in the file while you calculate and it's much safer.

119
00:08:57,410 --> 00:09:02,630
And also, yeah, you can check while you're calculating what is the result.

120
00:09:03,650 --> 00:09:07,790
Now, of course, we do not only want to write in the file, we also want to load.

121
00:09:08,240 --> 00:09:14,930
And for this, I will use the command and p note text, although to text.

122
00:09:16,010 --> 00:09:22,430
Now we must specify which file we want to load, and I just will load the data from one of our files

123
00:09:22,430 --> 00:09:28,700
and we have just created which is in the same directory so we can just write this and we must, of course,

124
00:09:29,090 --> 00:09:30,260
assign a name to it.

125
00:09:31,610 --> 00:09:33,500
So this will be our loaded list.

126
00:09:35,000 --> 00:09:37,300
And now I can check if this really worked.

127
00:09:37,370 --> 00:09:43,610
So I write node list and it gives me the array one, four, nine, 16 and twenty five.

128
00:09:44,690 --> 00:09:49,670
And of course, I could now continue and work with the data that we have just loaded.

129
00:09:50,300 --> 00:09:56,990
For example, just write a lot of lists zero and it will give me the first entry, which is one dot

130
00:09:56,990 --> 00:09:57,500
zero.

131
00:09:58,310 --> 00:10:00,470
So you see, loading data is pretty easy.

132
00:10:00,620 --> 00:10:07,280
If the data stored in the correct format, so I would always encourage you to use one of these methods

133
00:10:07,280 --> 00:10:11,090
so that you have your data one number in every line.

134
00:10:11,090 --> 00:10:13,970
So then there will be no problem when you load the data.

