import yaml def read_yaml(file_path): """ 读取 YAML 文件并返回 Python 对象。 参数: file_path (str): YAML 文件路径。 返回: data (dict): YAML 文件内容转换的 Python 对象。 """ with open(file_path, 'r', encoding='utf-8') as file: data = yaml.safe_load(file) return data def write_yaml(data, file_path): """ 将 Python 对象写入 YAML 文件。 参数: data (dict): 要写入 YAML 文件的 Python 对象。 file_path (str): 目标 YAML 文件路径。 """ with open(file_path, 'w', encoding='utf-8') as file: yaml.safe_dump(data, file, default_flow_style=False, allow_unicode=True, sort_keys=False) def update_yaml(file_path, key, value): """ 更新 YAML 文件中的指定键值对。 参数: file_path (str): YAML 文件路径。 key (str): 要更新的键。 value: 要更新的值。 """ data = read_yaml(file_path) data[key] = value write_yaml(data, file_path) def delete_key_yaml(file_path, key): """ 删除 YAML 文件中的指定键。 参数: file_path (str): YAML 文件路径。 key (str): 要删除的键。 """ data = read_yaml(file_path) if key in data: del data[key] write_yaml(data, file_path) if __name__ == '__main__': # 示例使用 yaml_file = 'keyword.yaml' data_to_write = { "开始按摩":("begin.mp3", 'begin:cupping:["heart", "kidney", "hepatobiliary", "lung", "spleen"]'), "旋转头":("砭石.mp3",'begin:stone_needle:["heart", "kidney", "hepatobiliary", "lung", "spleen"]'), "艾灸":("艾灸.mp3",'begin:cupping:["heart", "kidney", "hepatobiliary", "lung", "spleen"]'), "上一点点": ("little_up.mp3", "adjust:pose:move up:low"), "下一点点": ("little_down.mp3", "adjust:pose:move down:low"), "左一点点": ("little_left.mp3", "adjust:pose:move left:low"), "右一点点": ("little_right.mp3", "adjust:pose:move right:low"), "又一点点": ("little_right.mp3", "adjust:pose:move right:low"), "有一点点": ("little_right.mp3", "adjust:pose:move right:low"), "上移动一点点": ("little_up.mp3", "adjust:pose:move up:low"), "下移动一点点": ("little_down.mp3", "adjust:pose:move down:low"), "左移动一点点": ("little_left.mp3", "adjust:pose:move left:low"), "右移动一点点": ("little_right.mp3", "adjust:pose:move right:low"), "又移动一点点": ("little_right.mp3", "adjust:pose:move right:low"), "有移动一点点": ("little_right.mp3", "adjust:pose:move right:low"), "上一点": ("up.mp3", "adjust:pose:move up:mid"), "下一点": ("down.mp3", "adjust:pose:move down:mid"), "左一点": ("left.mp3", "adjust:pose:move left:mid"), "右一点": ("right.mp3", "adjust:pose:move right:mid"), "又一点": ("right.mp3", "adjust:pose:move right:mid"), "上移动一点": ("up.mp3", "adjust:pose:move up:mid"), "下移动一点": ("down.mp3", "adjust:pose:move down:mid"), "左移动一点": ("left.mp3", "adjust:pose:move left:mid"), "右移动一点": ("right.mp3", "adjust:pose:move right:mid"), "又移动一点": ("right.mp3", "adjust:pose:move right:mid"), "上一些": ("up.mp3", "adjust:pose:move up:high"), "下一些": ("down.mp3", "adjust:pose:move down:high"), "左一些": ("left.mp3", "adjust:pose:move left:high"), "右一些": ("right.mp3", "adjust:pose:move right:high"), "又一些": ("right.mp3", "adjust:pose:move right:high"), "往上": ("up.mp3", "adjust:pose:move up:high"), "往下": ("down.mp3", "adjust:pose:move down:high"), "往左": ("left.mp3", "adjust:pose:move left:high"), "往右": ("right.mp3", "adjust:pose:move right:high"), "太痛了": ("lift_up.mp3", "adjust:force:decrease:high"), "好痛啊": ("lift_up.mp3", "adjust:force:decrease:high"), "太疼了": ("lift_up.mp3", "adjust:force:decrease:high"), "好痛": ("lift_up.mp3", "adjust:force:decrease:mid"), "好疼": ("lift_up.mp3", "adjust:force:decrease:mid"), "轻一点": ("lift_up.mp3", "adjust:force:decrease:mid"), "太重了": ("lift_up.mp3", "adjust:force:decrease:mid"), "有点重": ("lift_up.mp3", "adjust:force:decrease:low"), "中了": ("lift_up.mp3", "adjust:force:decrease:low"), "太轻了": ("move_deeper.mp3", "adjust:force:increase:high"), "没感觉": ("move_deeper.mp3", "adjust:force:increase:high"), "重一点": ("move_deeper.mp3", "adjust:force:increase:mid"), "停止":("emergency_stop.mp3", "stop"), "停车": ("emergency_stop.mp3", "stop"), "就是这里": ("就是这里.mp3", ""), "就按这里": ("就是这里.mp3", ""), "多少钱": ("多少钱.mp3", ""), "你是谁": ("你是谁.mp3", ""), "怎么收费": ("你是怎么收费的.mp3", ""), } write_yaml(data_to_write, yaml_file) # 写入 YAML 文件 read_data = read_yaml(yaml_file) # 读取 YAML 文件 print(read_data) for key, value in read_data.items(): mp3_file, instruction = value print(f"{key}: {mp3_file}, {instruction}") # update_yaml(yaml_file, 'founded', 1910) # 更新键值对 # read_data = read_yaml(yaml_file) # print(read_data) # delete_key_yaml(yaml_file, 'location') # 删除键 # read_data = read_yaml(yaml_file) # print(read_data)