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
#数组去重
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项目,不报错了。