1 Star 0 Fork 0

赵艳超/py-scripts

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
import_struct_field.py 3.55 KB
一键复制 编辑 原始数据 按行查看 历史
zhaoyanchao 提交于 2023-12-14 09:29 . 修改
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()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/hibixby/py-scripts.git
git@gitee.com:hibixby/py-scripts.git
hibixby
py-scripts
py-scripts
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385