代码拉取完成,页面将自动刷新
import json
import warnings
from dataclasses import dataclass
from typing import Dict
import requests
from openpyxl.reader.excel import load_workbook
from model.consts import EXCEL_FILE_PATH, BASE_URL, HEADERS
from model.excel_utils import process_merged_cells
from model.json_utils import default_encoder
from model.model_def import LineStatistics
warnings.filterwarnings("ignore", category=UserWarning)
@dataclass
class MetricsElement:
key: str = ''
name: str = ''
script: str = ''
topicId: int = 0
dataItems: str = None
remark: str = ''
resultType: int = 1
resultRemark: str = ''
def parse_metrics_element(workbook, sheet_index) -> Dict[str, MetricsElement]:
# 先读取
sheet = workbook.worksheets[sheet_index]
merged_cells = process_merged_cells(sheet)
metrics_element_map = {}
# 行统计信息,用于调试数据缺失
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_key, metrics_element_name, metrics_element_key, metrics_element_script = (
row[5].value if row[5].value is not None else merged_cells.get((row_number, 6)),
row[7].value if row[7].value is not None else merged_cells.get((row_number, 8)),
row[8].value if row[8].value is not None else merged_cells.get((row_number, 9)),
row[9].value if row[9].value is not None else merged_cells.get((row_number, 10)),
row[10].value if row[10].value is not None else merged_cells.get((row_number, 11)))
if not (struct_name and field_key and metrics_element_name and metrics_element_key):
line_statistics.fail += 1
print(f"failed data {row_number}: {struct_name} {field_key}")
continue
struct_name, metrics_element_name = struct_name.replace('\n', ''), metrics_element_name.replace('\n', '')
if metrics_element_key in metrics_element_map:
print(f'duplicate metrics element key: {metrics_element_key}')
continue
metrics_element = MetricsElement()
metrics_element.key, metrics_element.name = metrics_element_key, metrics_element_name
metrics_element.script, metrics_element.remark = metrics_element_script, '结构:' + struct_name + ',字段:' + field_key
metrics_element_map[metrics_element_key] = metrics_element
print(f'line statistics: {line_statistics}')
return metrics_element_map
def save_metrics_element(metrics_element_map):
for metrics_element in metrics_element_map.values():
resp = requests.post(
url=BASE_URL + '/risk/sky/metrics/element',
data=json.dumps(metrics_element, default=default_encoder),
headers=HEADERS
)
print(f"resp: {resp.json()}")
def main():
# 解析
workbook = load_workbook(filename=EXCEL_FILE_PATH)
for sheet_index in range(len(workbook.worksheets)):
metrics_element_map = parse_metrics_element(workbook, sheet_index)
print(f'metrics element map {metrics_element_map}')
save_metrics_element(metrics_element_map)
if __name__ == '__main__':
main()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。