添加对python算法的支持
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
import numpy as np
|
||||
import os
|
||||
import re
|
||||
|
||||
|
||||
def config_read_double(filepath, section, key):
|
||||
"""
|
||||
从配置文件中读取指定节和键对应的浮点数值。
|
||||
|
||||
对应 Matlab 的 ConfigReadDouble 函数。
|
||||
|
||||
参数:
|
||||
filepath: 配置文件路径
|
||||
section: 节名 (例如 'Data storing', 'section2: Diameter calibration')
|
||||
key: 键名 (例如 'makeSave', 'Nominal')
|
||||
|
||||
返回:
|
||||
numpy.ndarray: 读取到的浮点数值数组;如果找不到或为空,返回空数组
|
||||
"""
|
||||
if not os.path.exists(filepath):
|
||||
return np.array([])
|
||||
|
||||
try:
|
||||
with open(filepath, 'r', encoding='utf-8', errors='ignore') as f:
|
||||
lines = f.readlines()
|
||||
except Exception:
|
||||
return np.array([])
|
||||
|
||||
in_target_section = False
|
||||
section_pattern = re.compile(r'^\s*\[\s*(.*?)\s*\]\s*$')
|
||||
key_pattern = re.compile(r'^\s*' + re.escape(key) + r'\s*=\s*(.*?)\s*$', re.IGNORECASE)
|
||||
|
||||
for line in lines:
|
||||
line_stripped = line.strip()
|
||||
|
||||
if not line_stripped or line_stripped.startswith('%'):
|
||||
continue
|
||||
|
||||
section_match = section_pattern.match(line_stripped)
|
||||
if section_match:
|
||||
current_section = section_match.group(1).strip()
|
||||
in_target_section = (current_section.lower() == section.lower())
|
||||
continue
|
||||
|
||||
if in_target_section:
|
||||
key_match = key_pattern.match(line_stripped)
|
||||
if key_match:
|
||||
value_str = key_match.group(1).strip()
|
||||
if not value_str:
|
||||
return np.array([])
|
||||
try:
|
||||
values = [float(v) for v in value_str.split()]
|
||||
return np.array(values)
|
||||
except ValueError:
|
||||
return np.array([])
|
||||
|
||||
return np.array([])
|
||||
Reference in New Issue
Block a user