from collections import OrderedDict import re from attrs import define @define class SEntry: tower_name: str = "" tower_height: float = 0 tower_type: str = "" mileage_in_s: int = 0 back_k: float = 0 forth_k: float = 0 altitude_off: float = 0 foundation_low: float = 0 # 基降 fitting: str = "" # 金具 is_tension_tower: bool = False class SFile: def __init__(self) -> None: self.tower_dic = None def open(self, s_file_path): self.tower_dic = OrderedDict() tower_dic = self.tower_dic with open(s_file_path, encoding="gbk") as s_file_obj: new_k = 0 last_k = -1 # next_is_tension_tower=False last_tower_name = "" for line in s_file_obj: norm_line = line.strip() if norm_line == "": continue if "首端转角号" in norm_line: new_k = float(norm_line.split(":")[-1].replace("E", "e")) # 模板系数 continue if "塔号" in norm_line: # next_is_tension_tower=True if last_tower_name != "": tower_dic[last_tower_name].is_tension_tower = True continue norm_entry = re.sub("\s+", ",", norm_line) sep_entry = norm_entry.split(",") tower_name = sep_entry[0] if tower_name in tower_dic: tower_dic[tower_name].forth_k = new_k # 更新耐张塔前侧k值。 continue s_entry = SEntry() # s_entry.is_tension_tower=next_is_tension_tower # next_is_tension_tower=False s_entry.tower_name = tower_name last_tower_name = tower_name s_entry.tower_type = sep_entry[6] s_entry.tower_height = float(sep_entry[7]) s_entry.mileage_in_s = float(sep_entry[1]) s_entry.back_k = last_k s_entry.forth_k = new_k s_entry.altitude_off = float(sep_entry[3]) s_entry.foundation_low = float(sep_entry[4]) s_entry.fitting = sep_entry[8] last_k = s_entry.forth_k tower_dic[tower_name] = s_entry tower_dic[list(tower_dic.keys())[-1]].forth_k = -1 tower_dic[list(tower_dic.keys())[0]].is_tension_tower = True tower_dic[list(tower_dic.keys())[-1]].is_tension_tower = True @define class Fitting: fitting_length_dic = {} def __init__(self, fitting_file_path): content = [] with open(fitting_file_path) as fitting_file: for line in fitting_file: norm_line = line.strip() if norm_line == "": continue norm_entry = re.sub("\s+", ",", norm_line) content.append(norm_entry.split(",")) ite = iter(content[7:]) for fit in ite: # 跳过前面7行 fit_name = fit[0] fit_parameter = next(ite) self.fitting_length_dic[fit_name] = float(fit_parameter[2]) / 1000