Reputation: 4461
I am confused howcome the variable temp_dict is available outside else condition? when I am saving the array to MongoDB which is setting the ObjectId on each item in the array. Json parser doesn't doesn't know how to serialize ObjectId.
def read_temporary_file(filename, has_header=False):
wb = load_workbook(filename=filename, use_iterators=True)
ws = wb.get_active_sheet() # ws is now an IterableWorksheet
headers = []
rows = []
documents = []
for row in ws.iter_rows(): # it brings a new method: iter_rows()
if has_header:
has_header = False
for cell in row:
headers.append(cell.internal_value)
else:
temp_dict = OrderedDict()
for cell in row:
temp_dict[cell.column] = cell.internal_value
rows.append(temp_dict)
documents.append(temp_dict)
print("Saving documents to MongoDB")
__save(documents)
print("Printing %s" % temp_dict)
return (headers, rows)
Upvotes: 0
Views: 204
Reputation: 376052
In Python, the entire function is the scope. Clauses like your else are not new scopes.
Upvotes: 3