编写yaml配置文件时的问题汇总

读取yaml文件时报错如下:

congfig.yaml 配置内容如下
log:
  log_level: 'DEBUG'
  log_name: "class_api_test_v0\logs\log.txt"
运行如下的代码:
 def yaml_handler():
    with open("E:/autotest/python_lemon_29/test_class_0530/class_api_test_v0\conf\config1.yaml",encoding="utf-8") as f:
        config=yaml.load(f)
        return config
if __name__=="__main__":
    print(yaml_handler())
报错如下:
C:\Python37\python.exe E:/autotest/python_lemon_29/test_class_0530/class_api_test_v0/common/yaml_handler.py
E:/autotest/python_lemon_29/test_class_0530/class_api_test_v0/common/yaml_handler.py:14: YAMLLoadWarning: calling yaml.load() without Loader=... is deprecated, as the default Loader is unsafe. Please read https://msg.pyyaml.org/load for full details.
  config=yaml.load(f)
Traceback (most recent call last):
  File "E:/autotest/python_lemon_29/test_class_0530/class_api_test_v0/common/yaml_handler.py", line 17, in 
    print(yaml_handler())
  File "E:/autotest/python_lemon_29/test_class_0530/class_api_test_v0/common/yaml_handler.py", line 14, in yaml_handler
    config=yaml.load(f)
  File "C:\Python37\lib\site-packages\yaml\__init__.py", line 114, in load
    return loader.get_single_data()
  File "C:\Python37\lib\site-packages\yaml\constructor.py", line 49, in get_single_data
    node = self.get_single_node()
  File "C:\Python37\lib\site-packages\yaml\composer.py", line 36, in get_single_node
    document = self.compose_document()
  File "C:\Python37\lib\site-packages\yaml\composer.py", line 55, in compose_document
    node = self.compose_node(None, None)
  File "C:\Python37\lib\site-packages\yaml\composer.py", line 84, in compose_node
    node = self.compose_mapping_node(anchor)
  File "C:\Python37\lib\site-packages\yaml\composer.py", line 133, in compose_mapping_node
    item_value = self.compose_node(node, item_key)
  File "C:\Python37\lib\site-packages\yaml\composer.py", line 84, in compose_node
    node = self.compose_mapping_node(anchor)
  File "C:\Python37\lib\site-packages\yaml\composer.py", line 133, in compose_mapping_node
    item_value = self.compose_node(node, item_key)
  File "C:\Python37\lib\site-packages\yaml\composer.py", line 64, in compose_node
    if self.check_event(AliasEvent):
  File "C:\Python37\lib\site-packages\yaml\parser.py", line 98, in check_event
    self.current_event = self.state()
  File "C:\Python37\lib\site-packages\yaml\parser.py", line 449, in parse_block_mapping_value
    if not self.check_token(KeyToken, ValueToken, BlockEndToken):
  File "C:\Python37\lib\site-packages\yaml\scanner.py", line 116, in check_token
    self.fetch_more_tokens()
  File "C:\Python37\lib\site-packages\yaml\scanner.py", line 251, in fetch_more_tokens
    return self.fetch_double()
  File "C:\Python37\lib\site-packages\yaml\scanner.py", line 655, in fetch_double
    self.fetch_flow_scalar(style='"')
  File "C:\Python37\lib\site-packages\yaml\scanner.py", line 666, in fetch_flow_scalar
    self.tokens.append(self.scan_flow_scalar(style))
  File "C:\Python37\lib\site-packages\yaml\scanner.py", line 1149, in scan_flow_scalar
    chunks.extend(self.scan_flow_scalar_non_spaces(double, start_mark))
  File "C:\Python37\lib\site-packages\yaml\scanner.py", line 1224, in scan_flow_scalar_non_spaces
    "found unknown escape character %r" % ch, self.get_mark())
yaml.scanner.ScannerError: while scanning a double-quoted scalar
  in "E:/autotest/python_lemon_29/test_class_0530/class_api_test_v0\conf\config1.yaml", line 5, column 13
found unknown escape character 'l'
  in "E:/autotest/python_lemon_29/test_class_0530/class_api_test_v0\conf\config1.yaml", line 5, column 32

分析问题:
因为配置文件中
log_name: “class_api_test_v0\logs\log.txt”
导致的yaml.scanner.ScannerError错误

因为 “class_api_test_v0\logs\log.txt” 本身就存在问题

解决方法:
1、修改为 log_name: class_api_test_v0\logs\log.txt
2、修改为 log_name: “class_api_test_v0\logs\log.txt”
修改后再次运行

C:\Python37\python.exe E:/autotest/python_lemon_29/test_class_0530/class_api_test_v0/common/yaml_handler.py
{'log1': {'log2': '123'}, 'log': {'log_level': 'DEBUG', 'log_name': 'class_api_test_v0\\logs\\log.txt'}}

你可能感兴趣的:(测试)