import os import re import sys import json # def is_number(str): # try: # float(str) # return True # except ValueError: # return False def is_number(string): """ 判断字符串是否为数字类型 """ pattern = re.compile(r'^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$') return bool(pattern.match(string)) def get_curve_unit(string): xUnit = '' yUnit = '' xPhysics = '' yPhysics = '' try: unit_dict = json.loads(string) xUnit = unit_dict.get('xUnit') yUnit = unit_dict.get('yUnit') xPhysics = unit_dict.get('xPhysics') yPhysics = unit_dict.get('yPhysics') except: pass return xUnit, yUnit, xPhysics, yPhysics def get_curve_params(curvePath, decimal): """ 获取曲线文件最大最小值 """ valueList = [] with open(curvePath, 'r+', encoding='utf-8') as fr: lines = fr.readlines() for line in lines: if line.__contains__(','): yValue = line.split(',')[1] if(is_number(yValue)): valueList.append(float(yValue)) maxValue = round(max(valueList), decimal) minValue = round(min(valueList), decimal) xUnit, yUnit, xPhysics, yPhysics = get_curve_unit(lines[0]) curve_params = {"max":maxValue, "min":minValue, "xUnit":xUnit, "yUnit":yUnit, "xPhysics":xPhysics, "yPhysics":yPhysics} print(json.dumps(curve_params, ensure_ascii=False)) if __name__=='__main__': # 曲线文件路径 curvePath = sys.argv[1] # 保留小数点位数 try: decimal = int(sys.argv[2]) except: decimal = 3 get_curve_params(curvePath, decimal)