且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Python tips

更新时间:2022-09-13 19:36:17

0、Python Enhancement Proposal。(PEP,Python增强建议书)

0.1、Python中的注释

0.2、Python之禅。(import this

0.3、Python Cookbook 3rd Edition Documentation

0.4、第三方二进制扩展库:Unofficial Windows Binaries for Python Extension Packages

0.5、PyTutor

1、Python用print打印html文档时,若不打印协议首部,可能无法输出html文档。

1
print('Content-type: text/html\r\n')

2、Python2.7 搭建简单http server,只能解析静态文件。

1
python2.7  -m  SimpleHTTPServer 5678

3、Python3 搭建简单http server,只能解析静态文件。

1
python3 -m http.server 5678

4、Python2.7 搭建能处理python脚本的http server。

1
python2.7 -m CGIHTTPServer 5678

5、Python3 搭建能处理python脚本的http server。

1
2
3
4
5
from http.server import HTTPServer, CGIHTTPRequestHandler
port = 5678
httpd = HTTPServer(('', port), CGIHTTPRequestHandler)
print("Starting simple_httpd on port: " + str(httpd.server_port))
httpd.serve_forever()

6、Python的三种数据类型字典、列表、元组,分别用花括号、中括号、小括号表示。如:

1
2
3
4
字典:dic={'a':12'b':34}
列表:li=[1233]
集合:s = {1234}         #set是无序的无重复元素的列表
元组:tup=(1234)    #元组是不可更改的列表

8、Python打印不换行

(1)、通用方法

1
2
import sys
sys.stdout.write("no new line")

(2)、Python2 print 不换行(加逗号):print 'no new line',

(3)、Python3 print 不换行:print('no new line', end='')

9、Python 2.x 在使用help函数时,对内置函数一定要加引号

1
2
help(print)    #wrong
help('print')  #right

10、Python 模块的一般安装方法:

1
python setup.py install

12、全局变量若在函数内部被修改,会被编译器认为是局部变量,解决办法是在函数内用global声明这个变量。(参考这里

13、Python打印异常信息。

1
2
3
4
try:
  #do someting
except:
  print sys.exc_info()[0],sys.exc_info()[1]  #一般来说这样就足够了

这样更好:

1
2
3
4
5
6
try:
    #do someting
except:
    import traceback
    print(traceback.format_exc())
    traceback.print_exc()    #约等于上句


14、TypeError: 'str' object is not callable ,可能解决方案

15、以Windows Service的方式运行Python程序

16、Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)

17、PyQt学习入门

18、Python读取ini文件 。

22、python用win32com模拟浏览器no module named win32com.client错误解决InternetExplorer.Application的成员

24、将qt4的图形界面文件转化为python格式

25、python中的urlencode与urldecode

26、py2exe打包选项

27、Win7 64位编译Python扩展解决”error: Unable to find vcvarsall.bat”问题

28、几个Python配置工具简介:setuptools、pip、virtualenv 

28.1、Python 包管理工具解惑 。

29、python中获取python版本号的方法

30、查看python的搜索路径。

1
2
>>> import sys
>>> print sys.path

31、python 的日志logging模块

33、Python中计时器/定时器/计划任务,Timer/sched/APScheduler,参考这里。注意这篇博文中所用APScheduler应该为V2,V3的APScheduler有很多改动。

34、2015-5-7决定向Python3迁移。

35、Python3中str与bytes转换:The bytes/str dichotomy in Python 3

36、安装IPython

1
pip install ipython

安装IPython的Tab补全插件

1
pip install pyreadline

37、Python自定义排序

(1)、python 内建排序 HOW TO

(2)、Python中sorted()方法的用法

38、Python中configparser的bom问题,将'utf8'换为'utf-8-sig'即可。参见:configparser读取含有中文的配置(Windows)

39、whell文件(名)的格式:PEP 0427 -- The Wheel Binary Package Format 1.0

39.1、本机python的兼容性可以用这样查看:({python tag}-{abi tag}-{platform tag})

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
>>> import pip
>>> from pprint import pprint
>>> pprint(pip.pep425tags.get_supported())
[('cp34''none''win_amd64'),
 ('py3''none''win_amd64'),
 ('cp34''none''any'),
 ('cp3''none''any'),
 ('cp33''none''any'),
 ('cp32''none''any'),
 ('cp31''none''any'),
 ('cp30''none''any'),
 ('py34''none''any'),
 ('py3''none''any'),
 ('py33''none''any'),
 ('py32''none''any'),
 ('py31''none''any'),
 ('py30''none''any')]

40、Python内置模块/函数C代码查看:https://hg.python.org/cpython/branches

41、Python内存相关(gc):gc模块–Python内存释放Python深入06 Python的内存管理 。

42、Python 更改cmd中的字色 。

43、好玩的运算精度问题。

1
2
3
4
5
6
7
8
9
10
>>> 33/22
1.5
>>> 3.3/2.2
1.4999999999999998
>>> 33/15
2.2
>>> 3.3/1.5
2.1999999999999997
>>> 2-1.1
0.8999999999999999

44、Python not 对象True/False 的问题:Why is “if not someobj:” better than “if someobj == None:” in Python?

45、怎样忽略警告(不打印烦人的警告)。warnings.filterwarnings

1
2
import warnings
warnings.filterwarnings("ignore")

46、Python打印到终端同时记录到文件(tee)。(How do I duplicate sys.stdout to a log file in python?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Tee(object):
    def __init__(self):
        self.terminal = sys.stdout
        self.log = open("log.log""a")
         
    def __del__(self):
        sys.stdout = self.terminal
        self.log.close()
 
    def write(self, message):
        self.terminal.write(message)
        self.log.write(message)  
        self.log.flush()
         
sys.stdout = Tee()
 
print('HaHaHa')


N、...


*** walker * Updated 2017-05-07 ***

本文转自walker snapshot博客51CTO博客,原文链接http://blog.51cto.com/walkerqt/1212937如需转载请自行联系原作者


RQSLT