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)

Mysql-Python

安装Mysql数据库

1.pip install Mysql-Python

1
2
3
building '_mysql' extension
error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual
C++ Build Tools": http://landinghub.visualstudio.com/visual-cpp-build-tools

问题:python很多包都是通过C或C++写的,因此需要C++编译工具,

解决:安装C++编译或安装最新版的Visual Studio或是安装已经编译好的包

Mysql则需要安装mysql-python和mysqlclient

1
2
3
4
# mysql-python
https://www.lfd.uci.edu/~gohlke/pythonlibs/#mysql-python
# mysqlclient
https://www.lfd.uci.edu/~gohlke/pythonlibs/#mysqlclient

mysql-python

根据系统版本下载win_amd64或win32位的编译包,使用pip安装#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
pip install MySQL_python-1.2.5-cp27-none-win_amd64.whl
# 如果出现下面错误
ERROR: MySQL_python-1.2.5-cp27-none-win32.whl is not a supported wheel on this p
latform.
# 通过修改文件名来实现安装,文件名则根据Python版本进行修改,如我的版本是3.7
# 将
MySQL_python-1.2.5-cp27-none-win_amd64.whl
# 改成
MySQL_python-1.2.5-cp37-none-win_amd64.whl
# 即可
D:\Users\Administrator\Desktop\testss>python -V
Python 3.7.0

# D:\testss>python -V
Python 3.7.0

# D:\testss>pip install D:\testss\mysqlclient-1.4.6-cp37-cp37m-win_amd64.whl
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Processing d:\testss\mysqlclient-1.4.6-cp37-cp37m-win_amd64.whl
Installing collected packages: mysqlclient
Successfully installed mysqlclient-1.4.6

改名

参考: