我很困惑为什么变量 temp_dict 在其他条件之外可用?当我将数组保存到 MongoDB 时,它将在数组中的每个项目上设置 ObjectId。Json 解析器不知道如何序列化 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)