admin管理员组

文章数量:1610857

文章目录

  • 前言
  • 一、报错翻译
  • 二、报错代码
    • 1.读取yaml文件
    • 2.yaml文件
  • 三、修改后代码


前言

报错:UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0xb2 in position 8: invalid start byte


一、报错翻译

二、报错代码

1.读取yaml文件

代码如下(示例):

from string import Template

with open("blog.yml", encoding='utf-8') as f:
    read_yml = f.read()
    tempTemplate1 = Template(read_yml)
    y = tempTemplate1.safe_substitute({"user": "panpan", "psw": "99999"})
    print(y)

2.yaml文件

代码如下(示例):

name: 测试用例1
request:
    url: https://blog.csdn.net/weixin_44688529?type=blog
    method: POST
    headers:
        Content-Type: application/json
    json:
      username: $user
      password: $psw
validate:
      code: 200

运行结果:

定位问题:
去看yaml文件有没特殊字符,后面发现之前用例标题用字符串可以正常运行,改成中文后报错,后面编码格式改成 encoding='gb2312’即可正常运行

三、修改后代码

from string import Template

with open("blog.yml", encoding='gb2312') as f:
    read_yml = f.read()
    tempTemplate1 = Template(read_yml)
    y = tempTemplate1.safe_substitute({"user": "panpan", "psw": "99999"})
    print(y)

运行结果:

本文标签: 解决方法CodecDecodeUnicodeDecodeErrorUTF