0%

python3错误集不定时更新

snake

Photo by Austin Lowman on Unsplash

读写文件编码问题

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

改名

ip2Region报错 Index pointer not found

环境,Windows7,python3.7.0,ip2Region2.2.0,Flask1.1.0

情况,使用默认b-tree算法,配合Flask,第一次出现正常结果,刷新后则出现Index pointer not found,指针错误

原因,Flask生命周期或是其他原因导致ip2Region被关闭,(即使没有写searcher.close())

解决,更换其他算法即可,如binarySearch算法或是memory算法,如下

1
2
3
4
5
6
7
8
9
10
# 更换算法
algorithm = "memory"
searcher = Ip2Region(dbFile)
try:
# 算法使用的方法也要更换,如memorySearch,binarySearch
data = searcher.memorySearch(ip)
except Exception as e:
print(e)
# 跟换其他算法后关不关闭都不影响二次使用
searcher.close()

Flask

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 获取参数,get,post
# POST
request.form['username']

# GET
request.args.get('key', '')

# 自定义404错误,500等错误异常界面
@app.errorhandler(404)
def page_not_found(error):
return render_template('404.html'), 404

————————————————
参考链接:https://flask.palletsprojects.com/en/1.1.x/quickstart/#redirects-and-errors

三目运算

python中的三元表达式(三目运算符)
x = "变量1" if a>b else "变量2"

编码

Non-UTF-8 code starting with ‘\xe5’ in file

1
2
3
4
5
6
运行出现	Non-UTF-8 code starting with '\xe5' in file
解决,头部添加
# coding=utf8

————————————————
参考链接:https://stackoverflow.com/questions/23092176/syntaxerror-non-utf-8-code-starting-with-x91

参考: