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.
805 0
|
15天前
|
安全 Java 数据处理
Python网络编程基础(Socket编程)多线程/多进程服务器编程
【4月更文挑战第11天】在网络编程中,随着客户端数量的增加,服务器的处理能力成为了一个重要的考量因素。为了处理多个客户端的并发请求,我们通常需要采用多线程或多进程的方式。在本章中,我们将探讨多线程/多进程服务器编程的概念,并通过一个多线程服务器的示例来演示其实现。
|
15天前
|
程序员 开发者 Python
Python网络编程基础(Socket编程) 错误处理和异常处理的最佳实践
【4月更文挑战第11天】在网络编程中,错误处理和异常管理不仅是为了程序的健壮性,也是为了提供清晰的用户反馈以及优雅的故障恢复。在前面的章节中,我们讨论了如何使用`try-except`语句来处理网络错误。现在,我们将深入探讨错误处理和异常处理的最佳实践。
|
19天前
|
缓存 监控 Python
解密Python中的装饰器:优雅而强大的编程利器
Python中的装饰器是一种强大而又优雅的编程工具,它能够在不改变原有代码结构的情况下,为函数或类添加新的功能和行为。本文将深入解析Python装饰器的原理、用法和实际应用,帮助读者更好地理解和利用这一技术,提升代码的可维护性和可扩展性。
|
1天前
|
机器学习/深度学习 人工智能 数据可视化
Python:探索编程之美
Python:探索编程之美
9 0
|
1天前
|
机器学习/深度学习 人工智能 数据处理
Python编程的魅力与实践
Python编程的魅力与实践
|
2天前
|
SQL 关系型数据库 MySQL
第十三章 Python数据库编程
第十三章 Python数据库编程
|
2天前
|
存储 网络协议 关系型数据库
Python从入门到精通:2.3.2数据库操作与网络编程——学习socket编程,实现简单的TCP/UDP通信
Python从入门到精通:2.3.2数据库操作与网络编程——学习socket编程,实现简单的TCP/UDP通信