admin管理员组文章数量:1611160
文章目录
- 前言
- 一、报错翻译
- 二、报错代码
- 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
版权声明:本文标题:UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xb2 in position 8: invalid start byte解决方法 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dongtai/1728605071a1165347.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论