Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Python 3 compatibility to dataviz #182

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dataviz/full_source/dataviz.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion dataviz/tutorial_source/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions dataviz/tutorial_source/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ 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)

# Setup an empty list
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:
Expand All @@ -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__":
Expand Down
13 changes: 6 additions & 7 deletions website/_containers/dataviz/2013-01-04-part-1.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
```

<div class="panel panel-default">
<div class="panel-heading">For the Curious</div>
<div class="panel-body">
We were able to call the <code>.next</code> method on <code>csv_data</code> because it is a generator. We just call <code>.next</code> once, since headers are in the 1st and only row of our CSV file.
We were able to use <code>next(csv_data)</code> because <code>csv_data</code> is a generator. We just call <code>next(csv_data)</code> once, since headers are in the 1st and only row of our CSV file.
</div>
</div>

Expand Down Expand Up @@ -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:
Expand All @@ -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 `","`.
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -354,4 +354,3 @@ You can continue to play around; try `>>> help(parse.parse)` to see our docstrin
<li class="next"><a href="{{ get_url('/dataviz/part-2/') }}">Part 2: Graphing <span aria-hidden="true">&rarr;</span></a></li>
</ul>
</nav>