import re import json import os def parse_inp_file(file_path): commands_dict = {} comments_dict = {} with open(file_path, 'r') as f: lines = f.readlines() for line in lines: # 去除首尾空格并按空格分割 parts = line.strip().split(' ') if len(parts) == 2: # 只处理有一个参数的指令 command, value = parts # 检查参数是否为数字(包括科学计数法) if re.match(r'^-?\d+\.?\d*([eE][-+]?\d+)?$', value): commands_dict[command] = value # 读取注释文件 comments_file_path = 'visa-scpi-example/comments.json' if not os.path.exists(comments_file_path): with open(comments_file_path, 'w') as f: # 为空时,创建一个空的注释文件,将commands_dict的值以“”表示 comments_dict = {key: " " for key in commands_dict.keys()} json.dump(comments_dict, f) else: with open(comments_file_path, 'r') as f: comments_dict = json.load(f) # 保存为JSON文件 with open('visa-scpi-example/commands.json', 'w') as f: json.dump(commands_dict, f, indent=2) return commands_dict, comments_dict def save_comments(comments_dict): with open('visa-scpi-example/comments.json', 'w') as f: json.dump(comments_dict, f, indent=2) def update_inp_file(file_path, command, new_value): with open(file_path, 'r') as f: lines = f.readlines() updated_lines = [] for line in lines: parts = line.strip().split(' ') if len(parts) == 2 and parts[0] == command: updated_lines.append(f"{command} {new_value}\n") else: updated_lines.append(line) with open(file_path, 'w') as f: f.writelines(updated_lines)