瀏覽代碼

消除嵌套if分支,减少缩进

weartist 1 年之前
父節點
當前提交
3f8d4d699d
共有 1 個文件被更改,包括 29 次插入30 次删除
  1. 29 30
      data_verification.py

+ 29 - 30
data_verification.py

@@ -33,39 +33,38 @@ async def create_upload_file(file: UploadFile = File(...)):
     workbook = load_workbook(savename)
     # 获取指定的sheet
     sheet_names = workbook.sheetnames
-    if "脱贫户信息查询" in sheet_names:
-        sheet = workbook["脱贫户信息查询"]
-        title_row_num = 0
-
-        # 读取前5行,正常应该有字段名了
-        row_range = sheet[1:5]
-        for r in row_range:
-            for c in r:
-                if "户主编号" == c.value:
-                    title_row_num = c.row
-        # 获取字段名对应的列
-        title_dict = {}
-        title_rows = sheet[title_row_num]
-        # 遍历字段名所在行的所有单元格
-        for title_cell in title_rows:
-            x, y = coordinate_from_string(title_cell.coordinate)
-            title_dict[title_cell.value] = x
-
-        # print(title_dict)
-
-        # 开始读取表格内容
-        # print(sheet.max_row)
-        read_data(sheet, title_row_num + 1, sheet.max_row, title_dict)
-
-        # 保存文档
-        workbook.save(savename)
-
-        return {"code": 200, "msg": "分析完成,请点击下载查看分析结果", "fileName": file.filename}
-
-    else:
+    if "脱贫户信息查询" not in sheet_names:
         print("读取不到指定的sheet页")
         return {"code": 500, "msg": "读取不到指定的sheet页--脱贫户信息查询"}
 
+    sheet = workbook["脱贫户信息查询"]
+    title_row_num = 0
+
+    # 读取前5行,正常应该有字段名了
+    row_range = sheet[1:5]
+    for r in row_range:
+        for c in r:
+            if "户主编号" == c.value:
+                title_row_num = c.row
+    # 获取字段名对应的列
+    title_dict = {}
+    title_rows = sheet[title_row_num]
+    # 遍历字段名所在行的所有单元格
+    for title_cell in title_rows:
+        x, y = coordinate_from_string(title_cell.coordinate)
+        title_dict[title_cell.value] = x
+
+    # print(title_dict)
+
+    # 开始读取表格内容
+    # print(sheet.max_row)
+    read_data(sheet, title_row_num + 1, sheet.max_row, title_dict)
+
+    # 保存文档
+    workbook.save(savename)
+
+    return {"code": 200, "msg": "分析完成,请点击下载查看分析结果", "fileName": file.filename}
+
 
 def read_data(ws, start_row, end_row, title_dict):
     # 监测对象致(返)贫风险非最新设计的风险类型