编写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 <module>
    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'}}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值