Return to Video

Module 10 Part 4 JSON to CSV code along

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

more » « less
Video Language:
English
Duration:
17:13

English subtitles

Revisions