Python笔记

源代码

https://github.com/liqiang311/python.git

各类资料

文档

发布命令行工具

pyenv

ElementTree

Docker

PyNLPIR

ChatterBot

whoosh

hdfs3

requests

Theano

PyTorch

six

常用代码

文件头

1
2
#!/usr/bin/env python
# -*- coding: utf-8 -*-

utf8-reload(python2)

1
2
3
import sys
reload(sys)
sys.setdefaultencoding("utf-8")

注意:

  1. Python 3 与 python 2 有很大的区别,其中Python 3 系统默认使用的就是utf-8编码。
  2. 所以,对于使用的是Python 3 的情况,就不需要sys.setdefaultencoding(“utf-8”)这段代码。
  3. 最重要的是,Python 3 的 sys 库里面已经没有 setdefaultencoding() 函数了。

python3

1
2
import importlib
importlib.reload(sys)

类型转换

  • 字符转数字ord('A')
  • 数字转字符chr(65)
  • Unicode编码转换为utf-8u'ABC'.encode('utf-8')
  • utf-8编码转换wieldUnicode'abc'.decode('utf-8')

发布为exe文件

py代码,data_files中存放附带打包文件

1
2
3
4
5
6
from distutils.core import setup
#import glob
import py2exe

setup(console=["main.py"],
data_files=[(".",["cfg.ini",]),])

命令行中输入

1
python mypy2exe.py py2exe

会生成build和dist文件夹

发布为Linux可执行文件

使用PyInstaller

1
2
3
pip install pyinstaller
pyinstaller -F xxx.py
./dist/xxx

scrapy使用

Python抓包

UTF-8 & Unicode

link