0%

python3错误集不定时更新

读写文件编码问题

UnicodeDecodeError: ‘gbk’ codec can’t decode byte 0xae in position 553: illegal multibyte sequence

解决,添加指定编码

1
2
3
4
5
6
7
8
9
10
# 原
with open(filename, mode = 'r') as f:
for line in f:
print(repr(line))


# 修改
with open(filename, encoding='utf-8', mode = 'r') as f:
for line in f:
print(repr(line))

参考:UnicodeDecodeError: ‘gbk’ codec can’t decode byte 0xaa in position 553: illegal multibyte sequence · Issue #37 · rkern/line_profiler

YAML.load错误

YAMLLoadWarning: calling yaml.load() without Loader=… is deprecated, as the default Loader is unsafe. Please read https://msg.pyyaml.org/load for full details.

1
2
3
4
# 原
yaml.load(input)
# 修改
yaml.load(input, Loader=yaml.FullLoader)

参考:yaml.load()时总是出现警告:YAMLLoadWarning: calling yaml.load() without Loader=…_网络_如果没有梦想,那跟咸鱼有什么分别-CSDN博客

YAML模块找不到

ModuleNotFoundError: No module named ‘yaml’

解决,pip install pyyaml

参考:ModuleNotFoundError: No module named ‘yaml’ · Issue #291 · yaml/pyyaml

base64

a bytes-like object is required, not ‘str’

1
2
3
4
5
# 原
email['html'] = base64.b64encode(email.get('html'))

# 修改为
email['html'] = base64.b64encode(email.get('html').encode('utf-8')).decode('utf-8')

参考:TypeError: a bytes-like object is required, not ‘str’ (smtp_sen_email) · Issue #7 · sendpulse/sendpulse-rest-api-python

str转dict/如何将字符串转换成字典dict类型

1
2
import json
temp = json.loads(str)