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完整版.pdf》是一份全面且深入的Python学习资源,旨在帮助初学者和有经验的程序员进一步提升Python技能。这份资料覆盖了Python的多个核心概念,包括环境搭建、基本语法、数据类型、...
### Python学习笔记知识点详解 #### 一、Python简介与特性 **标题与描述解析:** "Python学习笔记.pdf" 的标题直接指出了文档的主题——Python的学习资料,而描述的重复表明该文档的主要内容即为Python的学习笔记...
在这份《python学习笔记.pdf》中,记录了Python编程的基础知识和一些技巧,内容涵盖了字符串处理、变量操作、数据结构、循环、条件判断等方面。以下是对学习笔记中提到知识点的详细说明。 ### 字符串处理 在Python...
【Python学习笔记-王纯业】是一份专为Python初学者设计的教程,由王纯业编撰。这个教程深入浅出地介绍了Python编程的基础知识,帮助初学者快速上手。下面将详细阐述该教程中可能包含的重要知识点,以及Python入门者...
由皮大庆编写的《Python学习笔记(强烈推荐)》以其丰富的内容、清晰的逻辑和实用的示例,成为了Python初学者的首选入门资料。在这份详尽的学习笔记中,作者从基础概念出发,逐步引导读者深入理解Python编程语言的核心...