Skip to content

Commit

Permalink
fixed close of file issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Lynn Root committed Mar 22, 2013
1 parent 3092c71 commit 365e478
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 18 deletions.
8 changes: 4 additions & 4 deletions dataviz/full_source/dataviz.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ def parse(raw_file, delimiter):
"""Parses a raw CSV file to a JSON-like object"""

# Open CSV file, and safely close it when we're done
with open(raw_file) as opened_file:
# Read CSV file
csv_data = csv.reader(opened_file, delimiter=delimiter)
opened_file = open(raw_file)

# Read the CSV data
csv_data = csv.reader(opened_file, delimiter=delimiter)

# Setup an empty list
parsed_data = []
Expand All @@ -39,7 +40,6 @@ def parse(raw_file, delimiter):
return parsed_data



def visualize_days(data_file):
"""Visualize data by day of week"""

Expand Down
7 changes: 4 additions & 3 deletions dataviz/tutorial_source/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ def parse(raw_file, delimiter):
"""Parses a raw CSV file to a JSON-like object"""

# Open CSV file, and safely close it when we're done
with open(raw_file) as opened_file:
# Read CSV file
csv_data = csv.reader(opened_file, delimiter=delimiter)
opened_file = open(raw_file)

# Read the CSV data
csv_data = csv.reader(opened_file, delimiter=delimiter)

# Setup an empty list
parsed_data = []
Expand Down
7 changes: 4 additions & 3 deletions dataviz/tutorial_source/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ def parse(raw_file, delimiter):
"""Parses a raw CSV file to a JSON-like object"""

# Open CSV file, and safely close it when we're done
with open(raw_file) as opened_file:
# Read CSV file
csv_data = csv.reader(opened_file, delimiter=delimiter)
opened_file = open(raw_file)

# Read the CSV data
csv_data = csv.reader(opened_file, delimiter=delimiter)

# Setup an empty list
parsed_data = []
Expand Down
21 changes: 13 additions & 8 deletions docs/_posts/2013-01-04-Part-1-Parse.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,16 @@ Just to quickly put these two lines in our `parse` function:
def parse(raw_file, delimiter):
"""Parses a raw CSV file to a JSON-line object"""

# Open CSV file, and safely close it when we're done
with open(raw_file) as opened_file:
# Open CSV file
open_file = open(raw_file)

# Read CSV file
csv_data = csv.reader(opened_file, delimiter=delimiter)
# Read the CSV data
csv_data = csv.reader(opened_file, delimiter=delimiter)

# Build a data structure to return parsed_data

# Close the CSV file

return parsed_data
```

Expand Down Expand Up @@ -159,11 +161,11 @@ Now let’s put the function together:
def parse(raw_file, delimiter):
"""Parses a raw CSV file to a JSON-like object"""

# Open CSV file, and safely close it when we're done
with open(raw_file) as opened_file:
# Open CSV file
opened_file = open(raw_file)

# Read CSV file
csv_data = csv.reader(opened_file, delimiter=delimiter)
# Read the CSV data
csv_data = csv.reader(opened_file, delimiter=delimiter)

# Setup an empty list
parsed_data = []
Expand All @@ -175,6 +177,9 @@ def parse(raw_file, delimiter):
for row in csv_data:
parsed_data.append(dict(zip(fields, row)))

# Close the CSV file
opened_file.close()

return parsed_data
```

Expand Down

0 comments on commit 365e478

Please sign in to comment.