From 21be32c968584e5883950c2db8ceb00e5e730980 Mon Sep 17 00:00:00 2001 From: Roger Hughes Date: Tue, 14 Jul 2015 10:42:29 -0400 Subject: [PATCH 1/2] Make to Python 3 compatible - Use `next` instead of `.next()` - Use `print()` instead of `print` --- dataviz/tutorial_source/parse.py | 6 +++--- website/_containers/dataviz/2013-01-04-part-1.md | 13 ++++++------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/dataviz/tutorial_source/parse.py b/dataviz/tutorial_source/parse.py index 1a757db0..0054fa8e 100644 --- a/dataviz/tutorial_source/parse.py +++ b/dataviz/tutorial_source/parse.py @@ -23,7 +23,7 @@ def parse(raw_file, delimiter): # Open CSV file, and safely close it when we're done opened_file = open(raw_file) - + # Read the CSV data csv_data = csv.reader(opened_file, delimiter=delimiter) @@ -31,7 +31,7 @@ def parse(raw_file, delimiter): parsed_data = [] # Skip over the first line of the file for the headers - fields = csv_data.next() + fields = next(csv_data) # Iterate over each row of the csv file, zip together field -> value for row in csv_data: @@ -48,7 +48,7 @@ def main(): new_data = parse(MY_FILE, ",") # Let's see what the data looks like! - print new_data + print(new_data) if __name__ == "__main__": diff --git a/website/_containers/dataviz/2013-01-04-part-1.md b/website/_containers/dataviz/2013-01-04-part-1.md index f51a2628..c5b30578 100644 --- a/website/_containers/dataviz/2013-01-04-part-1.md +++ b/website/_containers/dataviz/2013-01-04-part-1.md @@ -153,13 +153,13 @@ parsed_data = [] Good — we have a good data structure to add to. Now let’s first address our column headers that came with the CSV file. They will be the first row, and we’ll assign them to the variable `fields`: ```python -fields = csv_data.next() +fields = next(csv_data) ```
For the Curious
-We were able to call the .next method on csv_data because it is a generator. We just call .next once, since headers are in the 1st and only row of our CSV file. +We were able to use next(csv_data) because csv_data is a generator. We just call next(csv_data) once, since headers are in the 1st and only row of our CSV file.
@@ -189,7 +189,7 @@ def parse(raw_file, delimiter): parsed_data = [] # Skip over the first line of the file for the headers - fields = csv_data.next() + fields = next(csv_data) # Iterate over each row of the csv file, zip together field -> value for row in csv_data: @@ -211,7 +211,7 @@ def main(): new_data = parse(MY_FILE, ",") # Let's see what the data looks like! - print new_data + print(new_data) ``` We called our function `parse()` and gave it the `MY_FILE` global variable that we defined at the beginning, as well as the delimiter `","`. @@ -266,7 +266,7 @@ The output from the `(DataVizProj) $ python parse.py` should look like a bunch o '18:59', 'Date': '02/18/2003', 'X': '-122.445006858202', 'Resolution': 'NONE'}] ``` -You see this output because in the `def main()` function, and you explicitly say `print new_data` which feeds to the output of the terminal. You could, for instance, not print the `new_data` variable, and just pass the `new_data` variable to another function. Coincidently, that’s what [Part II]( {{get_url("Part-2-Graph/")}}) and [Part III]( {{get_url("/Part-3-Map/")}}) are about! +You see this output because in the `def main()` function, and you explicitly say `print(new_data)` which feeds to the output of the terminal. You could, for instance, not print the `new_data` variable, and just pass the `new_data` variable to another function. Coincidently, that’s what [Part II]( {{get_url("Part-2-Graph/")}}) and [Part III]( {{get_url("/Part-3-Map/")}}) are about! ### Explore further @@ -332,7 +332,7 @@ Let’s play with the parser function a bit: >>> new_data[0].values() ['FRAUD', '030203898', 'Tuesday', 'FORGERY, CREDIT CARD', 'NORTHERN', '37.8014488257836', '2800 Block of VAN NESS AV', '16:30', '02/18/2003', '-122.424612993055', 'NONE'] >>> for dict_item in new_data: -... print dict_item["Descript"] +... print(dict_item["Descript"]) ... DRIVERS LICENSE, SUSPENDED OR REVOKED LOST PROPERTY @@ -354,4 +354,3 @@ You can continue to play around; try `>>> help(parse.parse)` to see our docstrin - From 6aa018bab4170fd953d532ca1fd80c0eaca8b2dd Mon Sep 17 00:00:00 2001 From: Roger Hughes Date: Tue, 14 Jul 2015 10:45:24 -0400 Subject: [PATCH 2/2] Make Python 3 compatible Use `next()` instead of `.next()` --- dataviz/full_source/dataviz.py | 2 +- dataviz/tutorial_source/graph.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/dataviz/full_source/dataviz.py b/dataviz/full_source/dataviz.py index 318329eb..56aec4f8 100644 --- a/dataviz/full_source/dataviz.py +++ b/dataviz/full_source/dataviz.py @@ -28,7 +28,7 @@ def parse(raw_file, delimiter): parsed_data = [] # Skip over the first line of the file for the headers - fields = csv_data.next() + fields = next(csv_data) # Iterate over each row of the csv file, zip together field -> value for row in csv_data: diff --git a/dataviz/tutorial_source/graph.py b/dataviz/tutorial_source/graph.py index aca9908c..b32e1764 100755 --- a/dataviz/tutorial_source/graph.py +++ b/dataviz/tutorial_source/graph.py @@ -31,7 +31,7 @@ def parse(raw_file, delimiter): parsed_data = [] # Skip over the first line of the file for the headers - fields = csv_data.next() + fields = next(csv_data) # Iterate over each row of the csv file, zip together field -> value for row in csv_data: