1
00:00:05,030 --> 00:00:09,440
Hi, welcome to this new
video in our fifth week.

2
00:00:09,440 --> 00:00:12,860
So we're going to continue
with the idea of how to

3
00:00:12,860 --> 00:00:15,140
write and read
different file formats

4
00:00:15,140 --> 00:00:16,680
that we could use for,

5
00:00:16,680 --> 00:00:19,860
let's say, data visualization

6
00:00:19,860 --> 00:00:23,330
or for writing data that
could be used for others.

7
00:00:23,330 --> 00:00:25,660
In this case we're
going to be writing

8
00:00:25,660 --> 00:00:29,570
a table using CSV
file structure.

9
00:00:29,570 --> 00:00:34,140
So a CSV file stands for
a comma separated values.

10
00:00:34,140 --> 00:00:39,005
It's a file that understands
using delimiter,

11
00:00:39,005 --> 00:00:41,740
which in this case is
going to be the comma that

12
00:00:41,740 --> 00:00:44,945
we are actually in fact
trying to construct a table,

13
00:00:44,945 --> 00:00:49,015
that has columns and
rows, potentially,

14
00:00:49,015 --> 00:00:52,030
and this is an optional part
of it might have a header,

15
00:00:52,030 --> 00:00:53,660
So as you can see
here on the right,

16
00:00:53,660 --> 00:00:55,720
we have a table
that has a header

17
00:00:55,720 --> 00:00:58,380
specifying we have the X
coordinate, the Y coordinate,

18
00:00:58,380 --> 00:01:00,920
the Z coordinate, and then we

19
00:01:00,920 --> 00:01:03,840
have values that are
for each one of those.

20
00:01:03,840 --> 00:01:06,280
The CSV file, as we
can see on the bottom,

21
00:01:06,280 --> 00:01:09,240
really is very much
like a text file,

22
00:01:09,240 --> 00:01:12,040
but we're using those
commas with an intention,

23
00:01:12,040 --> 00:01:14,820
and we're going to see when
we open a file like this,

24
00:01:14,820 --> 00:01:17,015
something like Google
Sheets or Excel,

25
00:01:17,015 --> 00:01:19,070
that this file is going
to be interpreted

26
00:01:19,070 --> 00:01:21,465
correctly as a table.

27
00:01:21,465 --> 00:01:25,130
So let's see how we
can actually do that.

28
00:01:25,130 --> 00:01:26,630
We're going to use
the same class,

29
00:01:26,630 --> 00:01:29,870
the createWriter("Data.csv"),
and we're

30
00:01:29,870 --> 00:01:31,410
going to print lines
in the same way.

31
00:01:31,410 --> 00:01:33,610
But in this case we're
going to be combining

32
00:01:33,610 --> 00:01:36,090
variables and using the comma in

33
00:01:36,090 --> 00:01:38,110
between them to really construct

34
00:01:38,110 --> 00:01:41,710
something that could be
understood as a CSV file.

35
00:01:41,710 --> 00:01:43,890
So we're definitely
going to be working with

36
00:01:43,890 --> 00:01:46,085
that comma as a separator,

37
00:01:46,085 --> 00:01:48,870
the delimiter between values.

38
00:01:48,870 --> 00:01:52,150
So let's jump into the code
and see what we can do here.

39
00:01:52,150 --> 00:01:55,850
So let's jump straight
into processing.

40
00:01:55,850 --> 00:01:58,630
We're going to once
again write a output

41
00:01:58,630 --> 00:02:07,320
, there's going to be a writer.

42
00:02:07,320 --> 00:02:11,710
We're going to call this
data.csv this time.

43
00:02:12,290 --> 00:02:17,480
As simple as that, we will
be creating a CSV file.

44
00:02:18,710 --> 00:02:21,240
Let's create a header.

45
00:02:21,240 --> 00:02:26,760
So output.printline.

46
00:02:28,370 --> 00:02:31,185
Here, our header,

47
00:02:31,185 --> 00:02:33,720
we have the value of x,

48
00:02:33,720 --> 00:02:47,580
y, red, green, blue.

49
00:02:47,580 --> 00:02:49,520
Notice that I'm adding
commas to all those files.

50
00:02:49,520 --> 00:02:50,740
This is just a simple string,

51
00:02:50,740 --> 00:02:52,700
but I'm thinking of these values

52
00:02:52,700 --> 00:02:54,300
that are separated by commas

53
00:02:54,300 --> 00:02:56,800
just because the CSV file

54
00:02:56,800 --> 00:02:58,440
is going to be able
to understand that.

55
00:02:58,440 --> 00:03:01,940
Let's imagine that what
do we want to record?

56
00:03:01,940 --> 00:03:05,055
We want to record
some variables,

57
00:03:05,055 --> 00:03:07,840
x and y are going to be
float point variables.

58
00:03:07,840 --> 00:03:09,060
Red, green, and blue are

59
00:03:09,060 --> 00:03:10,705
going to be
potentially integers.

60
00:03:10,705 --> 00:03:13,710
Let's do it for a series
of maybe 500 objects.

61
00:03:13,710 --> 00:03:19,760
So for i in range 500,

62
00:03:19,760 --> 00:03:24,420
and we're going to
create an x variable,

63
00:03:24,420 --> 00:03:30,145
it's going to be
random.randrange

64
00:03:30,145 --> 00:03:34,400
between zero and 1200

65
00:03:34,400 --> 00:03:36,220
which is going to be
the size of our canvas.

66
00:03:36,220 --> 00:03:38,880
You can use this
canvas variable.

67
00:03:38,880 --> 00:03:40,740
I'm going to try to think

68
00:03:40,740 --> 00:03:43,400
that what I'm doing is
creating coordinates of

69
00:03:43,400 --> 00:03:49,290
let's say 500 random elements
or circles in the screen.

70
00:03:49,990 --> 00:03:55,605
We have our module import
random here available,

71
00:03:55,605 --> 00:03:57,795
so we can use the
random function.

72
00:03:57,795 --> 00:04:00,470
Make sure that you
have that as well.

73
00:04:00,480 --> 00:04:04,600
Down here, we're going to
do the same thing for y.

74
00:04:04,600 --> 00:04:09,330
In this case we're going
to use the random_height.

75
00:04:13,550 --> 00:04:17,040
Now, let's just do an r,

76
00:04:17,040 --> 00:04:20,230
this is going to start for red.

77
00:04:20,660 --> 00:04:24,960
Let's just copy paste
this line once more.

78
00:04:26,620 --> 00:04:29,240
In this case we
will have a value,

79
00:04:29,240 --> 00:04:33,010
but it's a random value
between zero and 255.

80
00:04:33,010 --> 00:04:36,015
That's for a random
red channel color.

81
00:04:36,015 --> 00:04:37,880
We can do the same thing for

82
00:04:37,880 --> 00:04:42,000
two more times for R, G, and B.

83
00:04:42,230 --> 00:04:45,540
So everything is
random at this point.

84
00:04:45,540 --> 00:04:49,130
If we would like to
see graphically,

85
00:04:49,130 --> 00:04:53,425
we can use fill for r,

86
00:04:53,425 --> 00:04:55,620
g,b so that we have

87
00:04:55,620 --> 00:04:57,945
a visual representation
of what we are doing.

88
00:04:57,945 --> 00:05:01,215
So we can do an ellipse
in the coordinates x,

89
00:05:01,215 --> 00:05:04,930
y, 20, 20.

90
00:05:05,780 --> 00:05:08,960
But what we're
actually trying to do

91
00:05:08,960 --> 00:05:15,020
is write all this
information to our file.

92
00:05:15,500 --> 00:05:18,020
We've seen this in
previous videos.

93
00:05:18,020 --> 00:05:20,220
We can actually use
random variables to

94
00:05:20,220 --> 00:05:23,805
construct 500 random
spheres or circle.

95
00:05:23,805 --> 00:05:25,980
Often the data that
we will obtain

96
00:05:25,980 --> 00:05:27,920
from outside might
not be random.

97
00:05:27,920 --> 00:05:30,000
So it's going to be probably
a lot more interesting data

98
00:05:30,000 --> 00:05:31,500
than the one that
we're creating here.

99
00:05:31,500 --> 00:05:33,565
But we're learning how
to write first a file,

100
00:05:33,565 --> 00:05:35,850
and then we're going to
learn how to read it.

101
00:05:36,680 --> 00:05:40,680
Here's where it could
actually get tricky,

102
00:05:40,680 --> 00:05:46,540
where we actually do
output.printline.

103
00:05:48,120 --> 00:05:54,640
This line we want to convert
to a string, the variable x.

104
00:05:54,640 --> 00:05:59,350
Then we want to add
to that a comma.

105
00:06:00,260 --> 00:06:04,740
Then we want to repeat
this sequence for

106
00:06:04,740 --> 00:06:08,600
all the different
variables we have.

107
00:06:08,600 --> 00:06:15,910
We could do that side by side
like this and do it for y.

108
00:06:16,790 --> 00:06:21,425
Some people really like seeing
it a little bit like this.

109
00:06:21,425 --> 00:06:24,990
So we have y,

110
00:06:24,990 --> 00:06:27,990
we have r,

111
00:06:27,990 --> 00:06:35,790
g, and b.

112
00:06:35,790 --> 00:06:38,820
So I really invited
you to format

113
00:06:38,820 --> 00:06:40,340
your documents in
the way that are the

114
00:06:40,340 --> 00:06:42,905
most readable for you, RGB.

115
00:06:42,905 --> 00:06:45,700
I sometimes sacrifice the amount

116
00:06:45,700 --> 00:06:49,180
of space the code might take.

117
00:06:52,550 --> 00:06:55,140
There we go. We're
closing that line.

118
00:06:55,140 --> 00:06:56,640
So that's the same print line as

119
00:06:56,640 --> 00:06:58,770
if we had everything
in sequence.

120
00:06:58,770 --> 00:07:01,360
We're just making sure
that we are adding

121
00:07:01,360 --> 00:07:05,620
the value as a string
with the comma for x,

122
00:07:05,620 --> 00:07:07,460
y, r, G, b and so on.

123
00:07:07,460 --> 00:07:11,310
So that is it for the output.

124
00:07:11,310 --> 00:07:17,040
Then we should be
within the loop here.

125
00:07:17,040 --> 00:07:18,210
Notice that in order

126
00:07:18,210 --> 00:07:23,220
to close the document,
what we have done before,

127
00:07:23,220 --> 00:07:30,630
which is output.flush,
and output.close,

128
00:07:33,400 --> 00:07:39,660
I need to get out of the loop.

129
00:07:39,660 --> 00:07:41,910
You don't want to be doing

130
00:07:41,910 --> 00:07:44,390
the closing of the
document within the loop,

131
00:07:44,390 --> 00:07:45,950
and this might be
a simple mistake

132
00:07:45,950 --> 00:07:49,130
because you're still
within the indentation.

133
00:07:49,130 --> 00:07:50,730
So make sure that you don't

134
00:07:50,730 --> 00:07:52,970
keep these two lines indented,

135
00:07:52,970 --> 00:07:55,600
therefore they exit the loop,

136
00:07:55,600 --> 00:08:00,765
and we the document at the end.

137
00:08:00,765 --> 00:08:03,810
I also want to make sure
that we do have a background

138
00:08:03,810 --> 00:08:06,630
because we're drawing these
ellipses in the screen.

139
00:08:06,630 --> 00:08:12,675
Let's just do a background
here, a black background.

140
00:08:12,675 --> 00:08:14,440
So we're not doing the
draw loop right now,

141
00:08:14,440 --> 00:08:18,780
we're just drawing 500 circles

142
00:08:18,780 --> 00:08:22,740
in the screen at random
locations. That's fine.

143
00:08:22,740 --> 00:08:25,920
That work. We can see
them and we should

144
00:08:25,920 --> 00:08:30,540
have in this point a CSV file.

145
00:08:30,540 --> 00:08:34,030
So I'm going to go into

146
00:08:35,160 --> 00:08:42,645
a text or a Google Sheets
document where we can open,

147
00:08:42,645 --> 00:08:50,200
let's just drag and
drop that CSV file.

148
00:08:50,200 --> 00:08:51,680
Once you load a CSV file,

149
00:08:51,680 --> 00:08:54,000
you'll see what you have here.

150
00:08:54,000 --> 00:08:56,220
What we have is first,

151
00:08:56,220 --> 00:09:02,780
it's going to be our header
row that contains the x, y.

152
00:09:02,780 --> 00:09:06,200
Basically what we described
in this line up here.

153
00:09:06,200 --> 00:09:09,950
This line became the
first line of our file,

154
00:09:09,950 --> 00:09:14,745
and as we continue
printing the values here

155
00:09:14,745 --> 00:09:16,910
Google Sheets automatically
can understand

156
00:09:16,910 --> 00:09:20,630
the CSV file to be a
comma separated values.

157
00:09:20,630 --> 00:09:22,370
Comma separated
value, meaning that

158
00:09:22,370 --> 00:09:26,550
these units end up in their
own individual cells.

159
00:09:26,550 --> 00:09:30,200
So we have data that could
be perfectly suitable for

160
00:09:30,200 --> 00:09:32,420
something like visualizing
this data if we wanted

161
00:09:32,420 --> 00:09:34,460
to reconstruct the
drawing from this data.

162
00:09:34,460 --> 00:09:37,610
Or maybe start doing like
a scatter plot or any form

163
00:09:37,610 --> 00:09:39,640
of data visualization that

164
00:09:39,640 --> 00:09:42,360
we might think might be
useful for this data.

165
00:09:42,360 --> 00:09:44,760
Coming to a file like this has

166
00:09:44,760 --> 00:09:46,340
a lot of clarity of these are x,

167
00:09:46,340 --> 00:09:47,440
y coordinates and these are

168
00:09:47,440 --> 00:09:50,220
the color values that are
associated with those.

169
00:09:50,220 --> 00:09:51,260
With this we're going to

170
00:09:51,260 --> 00:09:52,620
leave it here and
we're going to be

171
00:09:52,620 --> 00:09:54,060
looking at how to read this file

172
00:09:54,060 --> 00:09:56,530
in the next video.
I'll see you there.