代码拉取完成,页面将自动刷新
import warnings
from dataclasses import dataclass
from typing import List, Dict
from openpyxl import load_workbook
import requests
import json
from model.consts import BASE_URL, HEADERS, EXCEL_FILE_PATH
from model.excel_utils import process_merged_cells
from model.json_utils import default_encoder
from model.model_def import LineStatistics, StructField, Struct
warnings.filterwarnings("ignore", category=UserWarning)
def parse_data_struct(workbook, sheet_index) -> Dict[str, List[StructField]]:
"""
解析数据结构函数
从指定的Excel文件中读取数据,并将数据结构映射存放在一个字典中。
返回值:
data_struct_map (dict): 数据结构映射的字典,将数据结构名称作为键,对应的值为数据字段键的列表
"""
# 打开 Excel 文件
# 风险模型表单
sheet = workbook.worksheets[sheet_index]
merged_cells = process_merged_cells(sheet)
data_struct_map: Dict[str, List[StructField]] = {}
# 行统计信息,用于调试数据缺失
line_statistics = LineStatistics()
# 读取数据
for row_number, row in enumerate(sheet.iter_rows(), start=1):
line_statistics.total += 1
# row,col 和 row[index] 中的index 下标不同,index 0开始, merged_cells 中 1开始
struct_name, field_name, field_key = (
row[5].value if row[5].value is not None else merged_cells.get((row_number, 6)),
row[6].value if row[6].value is not None else merged_cells.get((row_number, 7)),
row[7].value if row[7].value is not None else merged_cells.get((row_number, 8)))
if not (struct_name and field_name and field_key):
line_statistics.fail += 1
print(f"failed data {row_number}: {struct_name} {field_name} {field_key}")
continue
struct_name = struct_name.replace('\n', '')
if struct_name in data_struct_map:
data_struct_map[struct_name].append(StructField(field_name, field_key))
else:
data_struct_map[struct_name] = [StructField(field_name, field_key)]
line_statistics.success += 1
print(data_struct_map)
print(f'line statistics: {line_statistics}')
return data_struct_map
def save_data_structs(data_struct_map):
for struct_name, fields in data_struct_map.items():
struct = Struct()
struct.name, struct.key, struct.remark = struct_name, struct_name, struct_name
processed_field_keys = set()
for field in fields:
if field.field_key in processed_field_keys:
continue
struct_field = {
'key': field.field_key,
'name': field.field_name,
'type': 2,
'remark': field.field_name
}
if struct.fields is None:
struct.fields = [struct_field]
else:
struct.fields.append(struct_field)
processed_field_keys.add(field.field_key)
json_body = json.dumps(struct, default=default_encoder)
print(f'fields count : {len(struct.fields)} req: {json_body}')
resp = requests.post(
url=BASE_URL + '/risk/sky/struct',
data=json_body,
headers=HEADERS
)
print('resp: ', resp.json())
def main():
# 解析
workbook = load_workbook(filename=EXCEL_FILE_PATH)
length = 2 # len(workbook.worksheets)
for sheet_index in range(length):
struct_fields_map = parse_data_struct(workbook, sheet_index)
save_data_structs(struct_fields_map)
if __name__ == '__main__':
main()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。