python 常见关键字和语法(备忘)
来源:互联网 发布:淘宝打豆豆游戏怎么玩 编辑:程序博客网 时间:2024/05/12 17:38
经常忘记python某些语法,记录下方便自己下次查询。
1.数据结构类:
set操作:set([])add,remove, in , not in
list 操作: list(xx), append ,insert(index,item), remove, del arr[index], pop()法删除
map操作: .has_key() , .keys()
str 操作: ",".join(arr) ,将arr 列表用“,”链接成字符串, int(xx) , str(xx),float(xx) , .startswith()
2. 中文编码类(gbk为例)
第二行声明编码:
# -*- coding: gbk -*-
定义中文字符变量 value =u'中文', 在代码中中文需统一成unicode进行各种比较操作。
读取中文文本: m=f.readline() msg=u'' msg+=m.decode("gbk") or msg+=unicode(m, "gbk")
print 中文变量: msg =u'中文' ,直接print msg 即可。
写入文本: msg=u'中文' f.write(msg.encode("gbk"))
3. pdb 调试
3.1.断点设置与消除
b 显示所有断点
b function...
b xx.py:110
disable/enable 1 (断点号)
3.2 普通操作
r, l , c, p ,s ,....
4. 简易过滤IP地址
rip = re.compile(r"(\d{1,3}\.){3}\d{1,3}")
5. opt 参数解析
from optparse import OptionParser
def getArgs(): usage = "usage:%prog [options] arg \n ....help INFO....." parser = OptionParser(usage) parser.add_option("-f","--file" ,dest = "tracefile",help = "read from the tracedata. ") parser.add_option("-r","--redundancyControl",action="store_true",dest = "redundancyControl",help = "enable to ...; ") options,args = parser.parse_args() return parser,options,argsdef checkArgs(parser,options,args): global redundancyControl redundancyControl = options.redundancyControl global tracefile if options.tracefile: tracefile = options.tracefile else: print "-f ;no tracefile is specified!" parser.print_help() sys.exit(1)
- python 常见关键字和语法(备忘)
- Python语法备忘
- python语法备忘
- Python语法备忘(I)
- python语法备忘(II)
- Python语法基础03.标识符和关键字
- Python语法备忘-常用函数
- 备忘--python常见面试题
- python中的不常见语法(1)
- SQL语法(备忘)
- XSLT 学习记录(一些语法和知识,备忘)
- Python——常见错误备忘
- oracle基本语法(备忘)
- 备忘(三)XPath语法
- java基本语法(备忘)
- 语法、关键字和常量变量
- python关键字和非关键字参数(可变长参数)
- Golang语法总结(一):关键字和注释方法
- 带返回值的多线程
- 一步步学习微软InfoPath2010和SP2010--第三章节--表单设计基础:处理InfoPath布局、控件和视图(3)--InfoPath控件
- android数据库
- 线性回归介绍之七——回归诊断
- n个人围成一圈,从第1个人开始报数,每报到第m个人,则其出局,求最后出局的人的初始序号
- python 常见关键字和语法(备忘)
- IOCP 细节解说
- linux移植boa服务器错误解决办法
- 搜索引擎学习-实现
- 线性回归介绍之八——回归方程的评价
- php清除html标签
- 插入排序算法的python实现
- C# 打开指定目录并定位到文件
- 线性回归介绍之九——多重线性回归