Python tips

简介:

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 = [ 1 2 3 3 ]
集合:s  =  { 1 2 3 4 }          #set是无序的无重复元素的列表
元组:tup = ( 1 2 3 4 )     #元组是不可更改的列表

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
相关文章
|
数据采集 Python
用Python做爬虫时相关tips的参考文章集锦(refreshing)
1.Python 3.x中的urllib的改动 2.Python的hasattr() getattr() setattr() 函数使用方法详解 3.正则表达式 re.
811 0
|
18天前
|
存储 人工智能 数据处理
Python:编程的艺术与科学的完美交融
Python:编程的艺术与科学的完美交融
19 1
|
5天前
|
JSON 数据格式 开发者
pip和requests在Python编程中各自扮演着不同的角色
`pip`是Python的包管理器,用于安装、升级和管理PyPI上的包;`requests`是一个HTTP库,简化了HTTP通信,支持各种HTTP请求类型及数据交互。两者在Python环境中分别负责包管理和网络请求。
19 5
|
7天前
|
存储 Python 容器
Python高级编程
Python集合包括可变的set和不可变的frozenset,用于存储无序、不重复的哈希元素。创建集合可使用{}或set(),如`my_set = {1, 2, 3, 4, 5}`。通过add()添加元素,remove()或discard()删除元素,如`my_set.remove(3)`。
10 0
|
8天前
|
测试技术 Python
Python模块化方式编程实践
Python模块化编程提升代码质量,包括:定义专注单一任务的模块;使用`import`导入模块;封装函数和类,明确命名便于重用;避免全局变量降低耦合;使用文档字符串增强可读性;为每个模块写单元测试确保正确性;重用模块作为库;定期维护更新以适应Python新版本。遵循这些实践,可提高代码可读性、重用性和可维护性。
33 2
|
14天前
|
测试技术 调度 索引
python编程中常见的问题
【4月更文挑战第23天】
32 2
|
14天前
|
网络协议 算法 网络架构
Python网络编程之udp编程、黏包以及解决方案、tcpserver
Python网络编程之udp编程、黏包以及解决方案、tcpserver
|
15天前
|
编解码 JavaScript 前端开发
【专栏】介绍了字符串Base64编解码的基本原理和在Java、Python、C++、JavaScript及Go等编程语言中的实现示例
【4月更文挑战第29天】本文介绍了字符串Base64编解码的基本原理和在Java、Python、C++、JavaScript及Go等编程语言中的实现示例。Base64编码将24位二进制数据转换为32位可打印字符,用“=”作填充。文中展示了各语言的编码解码代码,帮助开发者理解并应用于实际项目。