print 'hello, %s' %'zs' print 'hello, {name}'.format(name='zs') print 'hello, %(name)s' %{'name':'zs'}
python解析URL中协议、host和端口的方法
import urllib protocol, s1 = urllib.splittype('http://www.freedom.com:8001/img/people') # ('http', '//www.freedom.com:8001/img/people') host, s2= urllib.splithost(s1) # ('www.freedom.com:8001', '/img/people') host, port = urllib.splitport(host) # ('www.freedom.com', '8001')
zip()函数
>>> a=['zs','ls','ww'] >>> b=[21,22,29] >>> c=zip(a,b) >>> c [('zs', 21), ('ls', 22), ('ww', 29)] >>> dict(c) {'ww': 29, 'ls': 22, 'zs': 21}
os库
os.path.exists() #判断文件是否存在
os.system() #执行系统命令
os.getcwd() #获取当前执行程序文件路径
os.listdir(os.getcwd()) #获取当前路径下文件列表
os.system() #执行系统命令
os.getcwd() #获取当前执行程序文件路径
os.listdir(os.getcwd()) #获取当前路径下文件列表
#随机数
import random
random.randint(12, 20) #生成的随机数n: [12, 20]
# 删除目录 import shutil shutil.rmtree(path)
字符串
# 字符串查找,index找不到会报错,find不会报错
'abc'.index('b') #1
'abc'.index('z') #报错
'abc'.find('b') # 1
'abc'.find('z') # -1
'abc'.index('b') #1
'abc'.index('z') #报错
'abc'.find('b') # 1
'abc'.find('z') # -1
#清空list del list[:] #方法1 list[:]=[] #方法2 #去除字符串末尾回车换行符 str.rstrip() #命令行参数传递,默认python文件名为sys.argv[0],则第一个参数为sys.argv[1] import sys if(len(sys.argv) > 1): print sys.argv[1] #执行系统命令 ① 程序返回运行结果code,无法获取程序返回字符串 code = os.system('ls') ② 将程序运行结果读入数组,仍然无法获取程序运行返回code tmp = os.popen("ls -l").readlines() for s in tmp: print s ③ 可以获取程序运行返回code。!!这里有个疑问如何阻塞主进程 result = commands.getstatusoutput("date") print result[0] # 输出运行状态码,如果返回值为0,则程序正常退出 print result[1] # 输出命令行返回值 ④ 可以返回程序运行code,可以阻塞主进程 p = subprocess.Popen( "date" ,shell = True) p.wait() print p.returncode
#python 2.X 定义枚举 class Enum(set): def __getattr__(self, name): if name in self: return name raise AttributeError Animals = Enum(["DOG", "CAT", "HORSE"]) print Animals.DOG
#逐行读取文件 with open('/opt/2012.log', 'r') as f: for line in f: print line
#通过HTTP下载文件——方法一 import urllib urllib.urlretrieve ("http://www.google.cn/images/nav_logo4.png", "/home/heipark/this.gz")
#通过HTTP下载文件——方法二 import urllib data = urllib.urlopen("http://www.google.cn/images/nav_logo4.png").read() f = file("c:/google.jpg","wb") f.write(data) f.close()
#通过HTTP下载文件——方法三(带进度) import urllib2 url = "http://download.thinkbroadband.com/10MB.zip" file_name = url.split('/')[-1] u = urllib2.urlopen(url) f = open(file_name, 'wb') meta = u.info() file_size = int(meta.getheaders("Content-Length")[0]) print "Downloading: %s Bytes: %s" % (file_name, file_size) file_size_dl = 0 block_sz = 8192 while True: buffer = u.read(block_sz) if not buffer: break file_size_dl += len(buffer) f.write(buffer) status = r"%10d [%3.2f%%]" % (file_size_dl, file_size_dl * 100. / file_size) status = status + chr(8)*(len(status)+1) print status, f.close()
# http授权访问(比如apache安全设置) url = "http://<用户名>:<密码>@1.2.3.4/xxxxx/xx" urllib.urlretrieve (url, "local_file_name.gz")
#根据key排序遍历dict rows = {} // rows do something for key in sorted(rows.keys()): print key dict_data={} sorted_list=sorted(dict_data.items(),key=lambda x:x[1],reverse=True)
#数组去重
a = [1,1,3,3,5]
unique_a = set(a)
print unique_a
a = [1,1,3,3,5]
unique_a = set(a)
print unique_a
# 列表推导 [ i for i in range(5) if i%2==0] # 列表推导结合enumerate def _treatment(pos, element): return '%d : %s' %(pos, element) seq = ['one', 'two', 'three'] print [ _treatment(i, e) for i, e in enumerate(seq)]
#递归遍历目录:方法一 import os for root, dirs, files in os.walk('D:/'): for file in files: print os.path.join(root, file) #递归遍历目录:方法二 import os for root, dirs, files in os.walk('D:/'): for fi in range(0,files.__len__()): print os.path.join(root,files[fi])
pyDev误报找不到cx_Oracle
在“import cx_Oracle”的时候报Error,提示找不到库,但实际可以正常运行。
解决:打开"preference"=> "pyDev" => "Interpreter - Python" => "Forced Builtins",添加"cx_Oracle",保存后Clean项目,不报错了。
相关推荐
Python学习笔记 .pdf Python学习笔记 .pdf Python学习笔记 .pdf Python学习笔记 .pdf Python学习笔记 .pdf Python学习笔记 .pdf Python学习笔记 .pdf Python学习笔记 .pdf Python学习笔记 .pdf Python学习笔记 .pdf ...
Python学习笔记
最新Python学习笔记,Day 3,总结性学习笔记,适合初学者归纳总结。
Python学习笔记--皮大庆,非常适合零基础入门。对应英文版本《How to think like a computer scientist》
最新Python学习笔记,Day 5,总结性学习笔记,适合初学者归纳总结。
Python学习笔记.pdf
王纯业的Python学习笔记非常适合初学者入门的Python教程
Python学习笔记--皮大庆
1、Python环境搭建 1.1 Python简介 1.2 Python语言的发展历程 1.3 Python语言的主要特点 1.4 Python应用场景 1.5 Python环境搭建 1.6 第一个Python程序 1.7 集成开发工具 2、Python语法基础 2.1 Python中的...
王纯业版《Python学习笔记》.pdf
python学习笔记1——(廖雪峰教程,菜鸟教程)python基础 python学习笔记1——(廖雪峰教程,菜鸟教程) >>> classmates = ['Michael', 'Bob', 'Tracy'] >>> len(classmates) 3 ⽤索引来访问list中每⼀个位置的元素...
python 学习笔记——线性回归预测模型.pdf python 学习笔记——线性回归预测模型.pdf python 学习笔记——线性回归预测模型.pdf python 学习笔记——线性回归预测模型.pdf python 学习笔记——线性回归预测模型.pdf ...
Python 学习笔记
Python基础入门教程,适合Python初学者,文档内容包括, 目录 前言 i 第一章 程序 1 1.1 程序 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.2 什么是调试 . . . . . . . . . . . . . . . ....
python学习笔记+源码练习,简单易懂,让你从入门到不放弃
王纯业的Python学习笔记
python学习笔记说明的PPT,讲解版,是用来给学员讲解python的演示PPT。