Permalink
Browse files

fixed close of file issue

  • Loading branch information...
1 parent 3092c71 commit 365e47804b1809e74ebffdbd72098b81cc38eaf8 Lynn Root committed Mar 22, 2013
@@ -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 = []
@@ -39,7 +40,6 @@ def parse(raw_file, delimiter):
return parsed_data
-
def visualize_days(data_file):
"""Visualize data by day of week"""
@@ -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 = []
@@ -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 = []
@@ -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
```
@@ -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 = []
@@ -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
```

0 comments on commit 365e478

Please sign in to comment.