【yaml】如何将中文直接写入yaml文件
1、场景
自动化测试中,需要将一些接口自动的写入到yaml中, 但是发现文件中显示的是Unicode字符
2、处理方法
import yaml def load_yaml_data(file_path): try: with open(file_path, 'r', encoding='utf-8') as f: data = yaml.safe_load(f.read()) except Exception as e: raise e return data def dump_yaml_data(file_path, data): try: with open(file_path, 'w', encoding='utf-8') as f: result = yaml.safe_dump(data, f, default_flow_style=False, indent=2, allow_unicode=True) except Exception as e: raise e return result
主要是在参数中加
allow_unicode=True