1
00:00:06,050 --> 00:00:07,596
Hi, welcome to this new video.

2
00:00:07,596 --> 00:00:12,890
In this video we're going to be
learning how to read data from a CSV.

3
00:00:12,890 --> 00:00:14,879
We just learned what a CSV file is.

4
00:00:14,879 --> 00:00:18,917
It's a comma separated value file, and

5
00:00:18,917 --> 00:00:25,580
we have constructed data with our
writer script that is a CSV file.

6
00:00:25,580 --> 00:00:27,072
So we actually have a CSV file.

7
00:00:27,072 --> 00:00:30,639
If you don't have one, go to the previous
video where we construct one.

8
00:00:30,639 --> 00:00:32,709
You can also use something
from the Internet, but

9
00:00:32,709 --> 00:00:35,364
it might not be formatted the same
way in which we have it here.

10
00:00:35,364 --> 00:00:39,647
Let's cover a few ways, because you're
going to realize that there's many ways in

11
00:00:39,647 --> 00:00:41,650
which you can read data.

12
00:00:41,650 --> 00:00:45,800
Often we're relying in Python, we're
relying with functionality that is either

13
00:00:45,800 --> 00:00:49,654
inbuilt into Python or external libraries
that might become a little bit more

14
00:00:49,654 --> 00:00:53,410
complicated, but certainly going to
offer us more functionality, right?

15
00:00:53,410 --> 00:00:58,354
So with processing, right,
we have an inbuilt reader function

16
00:00:58,354 --> 00:01:02,970
which we already covered in
order to read a TXT file.

17
00:01:02,970 --> 00:01:07,830
As you can see here, we have a reader
file that it's reading a CSV

18
00:01:07,830 --> 00:01:11,638
file we can go through
with the read line method.

19
00:01:11,638 --> 00:01:14,405
We can skip the header,
if you don't want to read the header,

20
00:01:14,405 --> 00:01:16,176
you would just want to read the data.

21
00:01:16,176 --> 00:01:20,031
You can basically use
that to read each line.

22
00:01:20,031 --> 00:01:25,170
And we already saw how to use a while
statement to loop through the data.

23
00:01:25,170 --> 00:01:28,906
The only difference here,
as you can see in this example,

24
00:01:28,906 --> 00:01:33,330
is that we're using the split line
dot split and saying separate this

25
00:01:33,330 --> 00:01:38,133
line wherever you see a comma, because we
know that comma is going to be the key

26
00:01:38,133 --> 00:01:42,552
value that separates one piece of
data from another piece of data.

27
00:01:42,552 --> 00:01:47,615
So the split line function is going to
allow us to access multiple entries and

28
00:01:47,615 --> 00:01:50,748
we have to use the different
index numbers.

29
00:01:50,748 --> 00:01:54,923
So data 0 is going to be the first entry,
data 1 is going to the second, and so

30
00:01:54,923 --> 00:01:55,886
forth,right?

31
00:01:55,886 --> 00:02:00,747
So we have this file for
you in the exercises examples,

32
00:02:00,747 --> 00:02:05,500
but we're going to learn
a different version of how to do

33
00:02:05,500 --> 00:02:10,430
this using an inbuilt
CSV library in Python.

34
00:02:10,430 --> 00:02:15,207
That introduces us to a new concept,
which is the width statement, right?

35
00:02:15,207 --> 00:02:16,972
And this is something that
you might come across so

36
00:02:16,972 --> 00:02:19,126
it's good that we discuss
a little bit what this is, right?

37
00:02:19,126 --> 00:02:23,714
So a width statement is a specific
statement that would allow us to open

38
00:02:23,714 --> 00:02:28,298
a file in this case and would give us
a space in which we could a context,

39
00:02:28,298 --> 00:02:33,389
if you want, in which those resources
are going to be available for us, right?

40
00:02:33,389 --> 00:02:37,491
So if we're trying to read the example
text file, as you can see on the right,

41
00:02:37,491 --> 00:02:40,523
the width statement it's going to
create a context in which

42
00:02:40,523 --> 00:02:42,636
that text file is in fact available.

43
00:02:42,636 --> 00:02:46,936
The great thing about the width statement
in Python is that it will actually take

44
00:02:46,936 --> 00:02:51,366
care of the cleanup automatically, would
actually do the cleanup or the sequence

45
00:02:51,366 --> 00:02:55,693
that might be required to kind of close
that resource once we finish reading it.

46
00:02:55,693 --> 00:02:57,857
So if we move forward with a code.

47
00:02:57,857 --> 00:03:02,918
It's a very clean way of operating within
a space in which we need those resources,

48
00:03:02,918 --> 00:03:05,665
access those resources and
then we move on and

49
00:03:05,665 --> 00:03:10,210
the programming language will
take care of the cleanup state.

50
00:03:10,210 --> 00:03:12,392
So let's see how that would actually look.

51
00:03:12,392 --> 00:03:15,910
It would look slightly different
if we use this kind of CSV.

52
00:03:15,910 --> 00:03:20,913
We have to import CSV in our code and
this would give us

53
00:03:20,913 --> 00:03:25,938
access to a different kind
of syntax csv.reader.

54
00:03:25,938 --> 00:03:29,710
We can actually access
the same CSV file here and

55
00:03:29,710 --> 00:03:35,772
we use next to move to the next line
similarly to what we were doing before.

56
00:03:35,772 --> 00:03:40,516
But in this case the way we kind
of skip to the next line is

57
00:03:40,516 --> 00:03:43,310
with the next keyword, right?

58
00:03:43,310 --> 00:03:48,187
And then the for loop becomes a little bit
easier because we do a four row, right?

59
00:03:48,187 --> 00:03:52,258
So each row could be
represented as an entity within

60
00:03:52,258 --> 00:03:56,435
the data that was obtained
from the reader, right?

61
00:03:56,435 --> 00:04:01,386
So similarly we have to access
entries like the index 0 entry will

62
00:04:01,386 --> 00:04:07,110
represent the x, index 1,
it will represent the y, and so forth.

63
00:04:07,110 --> 00:04:13,130
So we're going to see how to access
the data using this index information.

64
00:04:13,130 --> 00:04:18,018
So let's jump into processing and
put this together and

65
00:04:18,018 --> 00:04:21,500
think through it as we write the code.

66
00:04:21,500 --> 00:04:24,520
So here we are in processing, right?

67
00:04:24,520 --> 00:04:29,392
And the first thing that we want to do

68
00:04:29,392 --> 00:04:34,368
is import our CSV module, right?

69
00:04:34,368 --> 00:04:38,558
That's going to give us
access to the CSV reading

70
00:04:38,558 --> 00:04:42,596
functionality built into Python, right?

71
00:04:42,596 --> 00:04:43,248
And again,

72
00:04:43,248 --> 00:04:48,530
there's even more powerful versions of
this using other external libraries.

73
00:04:48,530 --> 00:04:52,648
But again, it's good to know that there's
multiple ways of doing the same thing.

74
00:04:52,648 --> 00:04:55,961
So let's start by creating
our with statement, right?

75
00:04:55,961 --> 00:05:00,072
So with open, right?

76
00:05:00,072 --> 00:05:04,950
So we're going to open the data.csv.

77
00:05:07,850 --> 00:05:13,275
So that's going to open that and
we're going to say us,

78
00:05:13,275 --> 00:05:18,449
this is basically saying
when we open this file,

79
00:05:18,449 --> 00:05:22,112
let's call it CSV file, right?

80
00:05:22,112 --> 00:05:26,378
So we're actually creating
a variable within this

81
00:05:26,378 --> 00:05:30,549
statement that is the CSV file variable,
right?

82
00:05:30,549 --> 00:05:37,845
And here data, that is the data
that is contained within that file,

83
00:05:37,845 --> 00:05:43,092
it's going to be using the csv.reader,
right?

84
00:05:43,092 --> 00:05:48,474
So notice that we're using, similar to how
we have been using random in the past,

85
00:05:48,474 --> 00:05:53,077
we are using CSV now, that's not
a variable that's the module that we

86
00:05:53,077 --> 00:05:58,114
imported that contains the reader
function and we can read this CSV file.

87
00:05:58,114 --> 00:06:01,948
The CSV file, remember,
is going to be what we opened here, right?

88
00:06:01,948 --> 00:06:05,538
And because we're doing it
within the width statement,

89
00:06:05,538 --> 00:06:10,174
this resource of the data containing
the CSV file will only be available in

90
00:06:10,174 --> 00:06:12,895
the block defined by the width statement.

91
00:06:12,895 --> 00:06:15,882
Anything that is indented here, right?

92
00:06:15,882 --> 00:06:20,332
So at this point, you see we
are operating within the loop or

93
00:06:20,332 --> 00:06:23,542
within this block, the width statement.

94
00:06:23,542 --> 00:06:25,227
This is not necessarily a loop, right?

95
00:06:25,227 --> 00:06:29,788
This is just an area in which,
similar to an if statement, but

96
00:06:29,788 --> 00:06:34,550
it's a condition in which
the resources are available.

97
00:06:34,550 --> 00:06:39,316
What kind of functionality can
we have we can use next, right?

98
00:06:39,316 --> 00:06:44,797
That's in the case that we basically
moving the head the same way in which we

99
00:06:44,797 --> 00:06:50,890
are starting to read the first line,
the second line, and so on, right?

100
00:06:50,890 --> 00:06:56,818
At this point, if you want,
we could print,

101
00:06:56,818 --> 00:07:04,782
let's print the data just to
see what we have so far, right?

102
00:07:04,782 --> 00:07:09,117
So right now what we have is not
data itself, we cannot see it.

103
00:07:09,117 --> 00:07:14,720
It's actually giving reference to this
specific kind of object, right, this data.

104
00:07:15,810 --> 00:07:18,240
So, how do we access the actual data?

105
00:07:19,250 --> 00:07:24,150
So let's do a loop.

106
00:07:24,150 --> 00:07:28,260
So for a row in data.

107
00:07:31,350 --> 00:07:35,153
So, we need to understand
that this data structure,

108
00:07:35,153 --> 00:07:40,330
which is in fact an object,
contains a series of rows, right?

109
00:07:40,330 --> 00:07:48,173
So let's just print now, Now,

110
00:07:48,173 --> 00:07:54,209
right, So let's see what we're getting.

111
00:07:54,209 --> 00:08:01,476
So here we are starting to see the data
that it's inside the object, right?

112
00:08:01,476 --> 00:08:06,669
And because this CSV file structure or
this kind of module

113
00:08:06,669 --> 00:08:12,612
is doing a little bit of content,
it's content aware in a way.

114
00:08:12,612 --> 00:08:16,279
It's going to to structure that data
already in a way that it's easier

115
00:08:16,279 --> 00:08:17,381
to access, right?

116
00:08:17,381 --> 00:08:21,665
And here we're going to start
addressing one of the topics that

117
00:08:21,665 --> 00:08:24,775
we haven't really covered in this course.

118
00:08:24,775 --> 00:08:28,267
But it's going to be
the central focus of course 2,

119
00:08:28,267 --> 00:08:33,441
which is collections and data structures
and in this case a list, right?

120
00:08:33,441 --> 00:08:35,964
But for
now we want to make sure that we access,

121
00:08:35,964 --> 00:08:38,438
similar to what we have done with tuples.

122
00:08:38,438 --> 00:08:41,361
How do we access an entry in a collection,
right?

123
00:08:41,361 --> 00:08:46,546
So let's imagine, or let's just
remember what we wrote last lesson.

124
00:08:46,546 --> 00:08:54,130
So X equals, it's a float,
we know it's a float from the row.

125
00:08:54,130 --> 00:08:58,118
And because we printed all the rows,
right?

126
00:08:58,118 --> 00:09:05,368
We want to access the first
entity within that row, right?

127
00:09:05,368 --> 00:09:10,055
So that should represent the x variable.

128
00:09:10,055 --> 00:09:13,731
Let's print it and
see if in fact that's working right?

129
00:09:13,731 --> 00:09:17,145
So you can see now that
we're only printing the x,

130
00:09:17,145 --> 00:09:21,074
which is the first coordinate
that we store in the file.

131
00:09:21,074 --> 00:09:24,787
We're going through all the rows and

132
00:09:24,787 --> 00:09:29,335
we are obtaining the value
of the x variable by

133
00:09:29,335 --> 00:09:34,830
accessing the first entry
within the row list.

134
00:09:34,830 --> 00:09:38,174
For now, we need to understand that
a list is very similar to a tuple.

135
00:09:38,174 --> 00:09:42,548
It has some differences that we're
going to be covering in course 2.

136
00:09:42,548 --> 00:09:48,404
But for now we can basically use the same
syntax to access entities within that.

137
00:09:48,404 --> 00:09:55,689
So if we have row, the x variable,
we can probably access all of them, right?

138
00:09:55,689 --> 00:09:59,830
Reconstruct what we have done for y.

139
00:09:59,830 --> 00:10:03,090
And in this case would
be the second entry.

140
00:10:05,690 --> 00:10:13,882
And here maybe you want to give a little
bit of space for r, g and b, right?

141
00:10:13,882 --> 00:10:21,703
So this is r, this is g,
And this is b, right?

142
00:10:21,703 --> 00:10:28,230
And here the numbers would be the second,
sorry, the third or fifth element.

143
00:10:28,230 --> 00:10:32,017
So knowing what we know the data,
just by looking at the data and

144
00:10:32,017 --> 00:10:34,671
understanding how many columns do we have.

145
00:10:34,671 --> 00:10:38,743
We have x and y representing first and
second entries, and

146
00:10:38,743 --> 00:10:42,827
the next three entries represent
the color data, right?

147
00:10:42,827 --> 00:10:47,742
So we no longer need the print.

148
00:10:47,742 --> 00:10:53,161
If we wanted to visualize this,
we can do what we did before.

149
00:10:53,161 --> 00:10:58,822
So r, g, b and ellipse,

150
00:10:58,822 --> 00:11:03,788
x, y, 20, 20.

151
00:11:03,788 --> 00:11:08,030
And this is exactly what
we done in the past.

152
00:11:08,030 --> 00:11:11,248
Let's see if this actually works,
there we go.

153
00:11:11,248 --> 00:11:12,745
So we reconstructed the drawing.

154
00:11:12,745 --> 00:11:18,383
This drawing was originally made,
created using random functions, right?

155
00:11:18,383 --> 00:11:22,564
So every time that you press play
it would be slightly different and

156
00:11:22,564 --> 00:11:26,244
you can actually save it to
a data file as we did last week.

157
00:11:26,244 --> 00:11:31,020
And now we can read it and reconstruct
it with the exact coordinates that

158
00:11:31,020 --> 00:11:34,454
the circles had and
the exact colors that they had.

159
00:11:34,454 --> 00:11:36,764
Even if they were random initially,

160
00:11:36,764 --> 00:11:41,704
we can reconstruct the data by saving it
into a CSV and loading it once again.

161
00:11:41,704 --> 00:11:44,924
So this is kind of a different
technique of doing it.

162
00:11:44,924 --> 00:11:47,319
We certainly wanted to
introduce the width statement.

163
00:11:47,319 --> 00:11:52,383
Which is something that you might see as
you kind of Google and kind of try to find

164
00:11:52,383 --> 00:11:57,913
examples of data visualization or any kind
of example that uses resources, external

165
00:11:57,913 --> 00:12:02,940
resources such as data files, CSV files or
JSON files or other kind of files.

166
00:12:02,940 --> 00:12:05,727
You probably will see this structure,
the width statement.

167
00:12:05,727 --> 00:12:09,236
So this is how we could use it.

168
00:12:09,236 --> 00:12:10,764
Again, the notion of lists,

169
00:12:10,764 --> 00:12:15,114
it's a very important concept that we're
going to be covering in future lessons.

170
00:12:15,114 --> 00:12:19,930
So I look forward to sharing that with you
in future videos, so I'll see you then.