-
MALE_1: I am going to
do a walk through on
-
the JSON to CSV assignment.
-
What I've got here,
-
I've started in pie
charm and I just added
-
our import CSV and import JSON,
-
and then according to
the assignment we need
-
to include within
the main function.
-
I've added the if__
name equals the main,
-
then we invoke main.
-
Let's go ahead and
start coding for main.
-
First thing we want to do
is open up the JSON file,
-
which is called
station-data.json.
-
I'll start off here and let's
-
do a file handle equals open.
-
We'll start typing in
station groups right there.
-
Station, at that again,
-
I should be getting
an auto complete,
-
for some reason I am not.
-
Station data. I know why.
-
Let's put in quotes.
-
There we go. That's better.
-
Now we're just going to
do this in read only,
-
so we'll just do an R mode
option there on the open.
-
That should open up our file.
-
Now JSON is essentially
dictionaries, has keys,
-
and the format of
a dictionary is
-
pretty much mimics a dictionary.
-
What we first want to do is
-
we need to parse
and load this from
-
the JSON module so we
-
can [inaudible] the
variable to hold the parse,
-
so into stations equals and
we'll invoke json.loads,
-
and we want to pass
in that file.read.
-
Auto complete can be really
obnoxious sometimes.
-
Get rid of the extra
parentheses. Now we got that.
-
Now we want to, well,
-
that's pretty much
all we want to
-
do in main except for we do want
-
to have a couple
more functions so
-
that we'll come back to
main and invoke a function.
-
We actually want to alter
the data and we need to
-
process the data
and then alter it
-
because we're going
from JSON to CSV.
-
Let's see what we
want to do here.
-
Actually, going back to the
assignment for a minute,
-
says the code should include
at least three functions.
-
Let's make sure we do that.
-
Let's first define
a function here.
-
We'll call it process stations.
-
And we'll just take an argument
here of the station data.
-
In the station data, well,
-
the first thing we can do is
create another file handle,
-
and let's just call
it, so it makes sense.
-
We'll go CSV.
-
I was going to do out file,
-
I guess that's fine too.
-
Out_file equals that.
-
We give it a name.
-
Sorry, we just gave it a name.
-
Now you want to open and then
-
we'll give the actual file name,
-
which will be outputfile.csv.
-
In this case we'll do a mode of
-
w as the file doesn't exist,
-
that will automatically
create it for us.
-
Screen of the variable and get
-
our keys from that station data,
-
the station data element(0).keys
-
which should be the headers.
-
That is a function there.
-
Now we want to do a CSV writer.
-
A variable called
CSV writer equals,
-
and then now we'll
use the CSV module
-
and we'll invoke
that top function
-
right there, the
dictionary writer.
-
We'll pass in that
file we just created
-
the out_file variable
and then we'll
-
indicate that our fieldnames
-
equal those keys that
-
we just put into
the names variable.
-
Now what we will want to
do is go into a four loop.
-
Four, and I like to imply for
each item or each station,
-
we don't type in each,
-
for each station,
-
again the station data.
-
We'll create a variable,
-
let me say, astation,
I guess equals.
-
Well actually let's
stop right there from
-
minute because we need
another function right now.
-
I'm going to just stub this in.
-
We don't have errors.
Let's actually
-
come back to main from minute,
-
make sure we get this complete.
-
We want to invoke
-
process stations and passing
that data right here,
-
this variable, that function.
-
I'd like to just include
-
a little message
after everything
-
is all done so we can
just print completed.
-
Good enough. Now we
actually want to
-
do probably at least one
or two more functions.
-
Well, we actually do need
-
two more functions because
that's part of the assignment.
-
If we look at the station data,
-
the assignment indicates some
-
of the things that we need
to do to clean up the data.
-
If we go back to the
assignment, it says,
-
let's see, if the value
type is a percentage,
-
then convert the value to a
-
floating point
with two decimals,
-
accuracy, then convert
back to a string.
-
Sixty two percent should
be converted 2.62.
-
If the value type is a number,
-
then removes from
the output a number.
-
Example 13,493 should be
-
converted to the same,
but without the comma.
-
That should be easy.
-
Let's do a function called,
-
let me just do change_percent.
-
This will take a value argument
-
and up that in for a minute.
-
Then we'll also do another
one called alter_values,
-
and that will also take
in argument for item it.
-
Just put it pass
-
here.
-
Let's first do change percent.
-
All we're going to do here is
look for that percent sign
-
and get rid of it and
-
then we're going to
convert this to a float,
-
convert it from a percentage
to a decimal thing.
-
Let's see here,
-
we'll do a verbal to read
the percent equals value.
-
Well, first of all we can do
-
a simple replace and
we'll do a replace.
-
Then we're looking for
-
the percent sign and
-
we want to replace
that with nothing,
-
so that way to do that.
-
Now, we want to
do a float value,
-
because the type here is
going to be a string.
-
We want to convert it
to a float and then we
-
will divide that by 100 to
convert it to a decimal form.
-
Wow, that take care of that.
-
Now, we also want the
point two precision.
-
We can use that with
the format function.
-
Should be pretty nice.
-
A couple of different
ways we can do that,
-
I think the format function
will be the easiest.
-
We can also do convert it to
-
integer and float could
do the same thing,
-
but this is the easiest so let's
-
do float value equals format.
-
We'll pass in the float
value and then we want to,
-
it says.2 precision,
so we can just.2fs.
-
If we want three
decimal precision,
-
we can go just
different precisions
-
that way if you're not
familiar with the format.
-
That function is done.
-
All we need to do is return
that back to the caller,
-
and I think we can mark
that when we're done.
-
Now, alter values we want
-
to look for the word percent
-
and we also want to look
at the word number.
-
Because if we look
at the json file,
-
depending on if the value
type is percent or number.
-
In there, you can go if item,
-
now the header let's call it
-
value type that equals percent.
-
We can do something
down the item.
-
Now we want to actually
modify the value of the item,
-
so value equals,
-
and then we will invoke
that function we just wrote
-
the change percent and we'll
pass in the item value.
-
I
-
got
-
it's tech error or
something here.
-
Let's see, value equals change.
-
Oh, I know, that's why.
-
Too many equals. If the
value type is not percent,
-
we need to handle the
other one which is
-
the value type equals number.
-
This one is really easy,
-
we could put this in function,
-
but don't really need to
-
because it's just a
one line thing here.
-
We could alter this item to
-
item value equals and
-
then we'll just
replace that comma.
-
We got value.replace.
-
Then we can do replacing
-
the comma and then we'll
replace it with nothing.
-
That could be it there.
-
Then we'll just return
item back to the caller.
-
Now, I think we're good there.
-
Now we can go back to our for
-
loop and just start
accessing those.
-
We just access this
function, alter value,
-
so for each station
in station data and
-
for astation equals
invoke alter values.
-
Pass in the station item
that's in our loop there.
-
Then all we really want to do at
-
this point is write
it out to the csv,
-
so csvwriter.writerow and I
-
send that variable astation
-
and I think that
will do the trick.
-
That should be it. Let's get
-
this to go and see how it runs.
-
It looks like I've
missed something up
-
here in the change percent,
-
I've got this unused variable.
-
I see what I did here.
-
I accidentally use value
inside here. We clean this up.
-
We want to get rid of
that percent sign.
-
That should fix that problem.
-
To replace percent
equals value.replace,
-
we've got that in here
and now we're going
-
to convert it to a float,
-
divide it by 100 to a decimal,
-
do a precision, return
it back to the caller.
-
I think we're good now.
Let's go ahead and run this.
-
Now, this is going to go into
-
a file called output file.csv.
-
We already have the
station-data.json,
-
I don't have the
output file here.
-
Let's go and run this.
-
Nice. We'll ran and
-
I just saw there's the
output file there.
-
I'm going to go ahead
and open this in finder.
-
I wonder if I can
just open it up.
-
We can go. There we have
it. That looks good.
-
Like I said, we can change
the precision and stuff here.
-
If you want to tweak that.
-
Go through the data there,
-
everything looks pretty good.
-
Then we could also
open this in Excel,
-
or numbers on Mac.
-
I hope you find that helpful.
-
That's it. Exit.