随笔分类 - Python
摘要:1 使用%来格式字符串Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->print("hello%s:%s"%("AAA","youaresonice"))2 使用zip来将两个list构造为一个dictCode highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.c
阅读全文
摘要:一 基本的异常处理Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->defTestTryException():try:f=open('myfile.txt')s=f.readline()f.close()i=int(s.strip())exceptIOErrorasioerror:print(ioerror)exceptValueErrorasvalueerror:print(valueerror)except:print(&
阅读全文
摘要:经常地我们需要编写跨平台的脚本,但是由于不同的平台的差异性,我们不得不获得当前所工作的平台(操作系统类型)。代码如下:Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->importplatformdefTestPlatform():print("----------OperationSystem--------------------------")#Windowswillbe:(32bit,WindowsPE)#Linux
阅读全文
摘要:使用:foldercleanup.py -d 10 -k c:\test\keepfile.txt c:\test表示对c:\test目录只保留最近10天的子文件夹和keepfile.txt中指定的子文件夹。代码:Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->importosimportos.pathimportdatetimedefgetOption():fromoptparseimportOptionParserdes="cl
阅读全文
摘要:If an object’s value can be modified, the object is said to be mutable. If the value cannot be modified,the object is said to be immutable. mutable 可变类型,例如 list,set,自定义类型(等价于C#中的引用类型); immutable 不可变类型,例如string,numbers等(等价于C#中的值类型);一 引用和拷贝(references and copies)当程序中使用=赋值操作符时,例如a=b,对于不可变的对象,a作为b的一个拷贝.
阅读全文
摘要:一 http的get和post get和post的区别:get是从服务器上获取数据,post是向服务器传送数据。(1)参数传输方式, GET提交,请求的数据会附在URL之后,以?分割URL和传输数据,多个参数用&连接;例 如:login.action?name=hyddd&password=idontknow&verify=%E4%BD%A0 %E5%A5%BD。如果数据是英文字母/数字,原样发送,如果是空格,转换为+,如果是中文/其他字符,则直接把字符串用BASE64加密,得出如: %E4%BD%A0%E5%A5%BD,其中%XX中的XX为该符号以16进制表示的ASC
阅读全文
摘要:一 python文件的encoding默认地,python的.py文件以标准的7位ASCII码存储,然而如果有的时候用户需要在.py文件中包含很多的unicode字符,例如.py文件中需要包含中文的字符串,这时可以在.py文件的第一行或第二行增加encoding注释来将.py文件指定为unicode格式。#!/usr/bin/env python# -*- coding: UTF-8 -*-s = "中国" # String in quotes is directly encoded in UTF-8.但是如果你的py文件是文本文件,且是unicode格式的,不指定# -*
阅读全文
摘要:一 MySQL模块安装下载:http://sourceforge.net/projects/mysql-python安装: python setup.py build (源码安装) python setup.py install支持:目前支持MySQL versions 3.23-5.1和Python versions 2.3-2.6二 MySQL操作过程1)import MySQLdb导入MySQLdb模块。2)conn = MySQLdb.connect()使用connect()来连接MySQL数据库,connect()用来和数据库建立连接,接收数个参数,返回连接对象.比较常用的参数包括h
阅读全文
摘要:很多常用的python函数或模块,经常需要查看帮助,很不方便。在python的交互命令行下使用help()或在python文件中调用help()函数可以很方便的查看帮助。一 查看所有的关键字:help("keywords")Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->HereisalistofthePythonkeywords.Enteranykeywordtogetmorehelp.andelifimportretu
阅读全文
摘要:一 python提供的xml支持2种工业标准的xml解析方法-SAX和DOM。SAX(simple API for XML),是基于事件处理的,当XML文档顺序地读入时,每次遇到一个元素会触发相应的事件处理函数来处理。DOM(Document Object Model),通过构建一个树结构来表现整个xml文档,一旦树被构建,可以通过DOM提供了接口来遍历树和提取相应的数据。python还提供了python独特的xml解析方法,相比于SAX和DOM更容易使用和更加快速,此方法为ElementTree。python的xml模块为:1)xml.dom.minidom2)xml.elementtree
阅读全文
摘要:打印出builtin的函数:Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->forbuiltinindir(__builtins__):if(not(builtin.find("Error")>=0orbuiltin.find("Warning")>=0orbuiltin.find("Exception")>=0)):print(builtin)结果:Code hi
阅读全文
摘要:列出常见类型的方法:Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->defListFunctions(lists):print("------------------------------------------")print(type(lists))foritemindir(lists):if(notitem.startswith("__")):print(item)#listl=[1,2,3]#o
阅读全文
摘要:webservice提供方:http://www.webxml.com.cn/zh_cn/web_services.aspx天气预报webservice:http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx一 使用urllib + xml.dom.minidom通过http get的方式来使用webservice:例如上海的天气:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getWeatherbyCityName?theCityName=58367http
阅读全文
摘要:使用os.environ来读取和修改环境变量:Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->importosprint(os.environ["TEMP"])mydir="c:\\mydir"os.environ["MYDIR"]=mydirprint(os.environ["MYDIR"])pathV=os.environ["PATH"]p
阅读全文
摘要:subprocess.Popen用来创建子进程。1)Popen启动新的进程与父进程并行执行,默认父进程不等待新进程结束。Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->defTestPopen():importsubprocessp=subprocess.Popen("dir",shell=True)foriinrange(250):print("otherthings")2)p.wait函数使得父进程
阅读全文
摘要:python解析命令行参数 [推荐使用optionparser]一手动对sys.argv[1:]解析需要自己使用string比较或者regex来解析。Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->importsysdefTestSys():forarginsys.argv[1:]:print(arg)二使用getopt模块来解析1. 处理所使用的函数叫getopt(),因为是直接使用import导入的getopt模块,所以要加上限定geto
阅读全文
摘要:python在Linux的安装1)查看是否已经安装which pythonwhereis pythonpython -V2)yum或apt来安装在Redhat系Linux上安装python, 执行: sudo yum install python3)源码安装下载 :http://www.python.org/ftp/python/3.1.3/Python-3.1.3.tgz解压 :sudo tar -xzvf ./Python-3.1.3.tgz 安装 : su ./configure make make install exit检查 :/usr/local/bin/python3.1 -V创
阅读全文
摘要:我们都知道python从2.x升级到3.x的过程中有一些不兼容的改动,但是有时还我们不得不将2.x的程序升级到3.x。主要不兼容如下图:移植过程:1)确保存在的代码有足够的测试覆盖。从2.x到3.x的升级工作量并不是很大,但是包含了很多的微小的不兼容,必须通过测试来确保升级后的程序仍然保持原有的功能。完全的测试覆盖不是被建议的,但是确实是需要的。2) 用python 2.6 的 -3 命令行选项来运行程序,从而发现和去除明显的不兼容问题。3)使用2to3转化工具来自动地将程序转化为3.x版本,很多的不兼容问题会在转化的过程中被fix,同时一些需要手动升级的不兼容会被指出。4)使用python
阅读全文
摘要:代码: (使用os.listdir)Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->importosdefListFilesToTxt(dir,file,wildcard,recursion):exts=wildcard.split("")files=os.listdir(dir)fornameinfiles:fullname=os.path.join(dir,name)if(os.path.isdir(fullname)
阅读全文
摘要:转自:http://www.cnblogs.com/huxi/archive/2010/07/04/1771073.html1. 正则表达式基础1.1. 简单介绍正则表达式并不是Python的一部分。正则表达式是用于处理字符串的强大工具,拥有自己独特的语法以及一个独立的处理引擎,效率上可能不如str自带的方法,但功能十分强大。得益于这一点,在提供了正则表达式的语言里,正则表达式的语法都是一样的,区别只在于不同的编程语言实现支持的语法数量不同;但不用担心,不被支持的语法通常是不常用的部分。如果已经在其他语言里使用过正则表达式,只需要简单看一看就可以上手了。下图展示了使用正则表达式进行匹配的流程:
阅读全文

浙公网安备 33010602011771号