显示标签为“python”的博文。显示所有博文
显示标签为“python”的博文。显示所有博文

2012年6月12日

Python-字串样式比对


Python-字串样式比对

首先必须先汇入re模组,有关regular expression的相关说明可参考http://en.wikipedia.org/wiki/Regular_expression
ex1: 比对字串样式:句首为Wellcome中间有空格或tab隔开,句子结尾是here。括号中的字串会抓取到match.groups()中。要取出其中的每个字串,可以用match.group(i),i为要取出找到的第几个字串。
>>> import re>>> match=re.match('Wellcome[ \t]*(.*)here','Wellcome to here')>>> dir(match)['__copy__', '__deepcopy__', 'end', 'expand', 'group', 'groupdict', 'groups', 'span', 'start']>>> print match.groups()('to ',)>>> match.group(1)'to '

ex2 取出字串样式为'/(.*)/(.*)/(.*)'括号中的字串>>> match=re.match('/(.*)/(.*)/(.*)','/usr/home/sample')>>> match.groups()('usr', 'home', 'sample')>>> match.group(1)'usr'>>>

2012年6月10日

Python-原始段落显示方法-三个单引号或双引号


Python-原始段落显示方法-三个单引号或双引号
一般指定一个单引号或双引号的字串给变数时,无法断行,要断行或空格需要使用特殊符号'\n' 及'\t'。
>>> s='\nHello \n Where are you \nHear Hear\n\n'>>> print s
Hello
Where are youHear Hear

当你要显示一个段落,不想用特殊符号且希望在显示时格式遭到破坏时,可以使用三个单引号或双引号将一个段落的字串包含在其中即可。>>> s='''... Hello... Where are you... Hear Hear...... '''>>> s'\nHello \n Where are you \nHear Hear\n\n'>>> print s
Hello
Where are youHear Hear

2012年6月9日

python 物件型态取得使用方法协助-help 指令


python 物件型态取得使用方法协助-help 指令
当你对python物件使用上有不清楚或想知道更多使用方法时,可利用dir或help的指令直接对已指定值的变数或方法套上这两个指令即可。
dir函式只是给出可使用的方法或属性名称,要详细了解这个方法或名称如何使用,就必须用help的函式了。


>>> S'Hello'>>> dir(S)['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__str__', 'capitalize', 'center', 'count', 'decode','encode', 'endswith', 'expandtabs', 'find', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

>>> help(S.split)Help on built-in function split:
split(...)
S.split([sep [,maxsplit]]) -> list of strings

Return a list of the words in the string S, using sep as the
delimiter string. If maxsplit is given, at most maxsplit
splits are done. If sep is not specified or is None, any
whitespace string is a separator.
>>>
利用help函式,会显示这个方法或函式的语法与说明。

2012年6月8日

python 物件型态取得使用方法协助-dir 指令


python 物件型态取得使用方法协助-dir 指令
当你对python物件使用上有不清楚或想知道更多使用方法时,可利用dir或help的指令直接对已指定值的变数或方法套上这两个指令即可。
当你呼叫内建的dir函式时,会传回来指定物件可用的属性及方法串列。

>>> S='hello'>>> dir(S)['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__str__', 'capitalize', 'center', 'count', 'decode','encode', 'endswith', 'expandtabs', 'find', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']>>>
dir 会显示此物件的所有方法串列,有底线的代表是此物件的实作,前后双底线的是python用于实作细节的命名模式,一般我们是用不到。串列中没有底线的名称是可以对此物件呼叫的方法或属性。

2012年6月6日

Python-物件型态-字串-2

Python-物件型态-字串-2 
字串运算时,原字串并未作任何改变。字串常用的方法有:


1.寻找子字串的偏移量
>>> S='Hello'
>>> S.find('el')
1
>>> S'Hello'


2.以另一个字串取代子字串的内容
>>> S.replace('el','ABC')
'HABClo'
>>> S'Hello'


3.以分界字元或字串将原字串切割成多个子字串串列
>>> S='aaa,bbb,ccc,ddd'
>>> S.split(',')
['aaa', 'bbb', 'ccc', 'ddd']
>>> S'aaa,bbb,ccc,ddd'


2012年5月31日

Python-物件型态-字串-1


Python-物件型态-字串-1

1.序列运算1.1 取得字串长度
>>> S='Hello'>>> len(S)5
1.2取得字串之中的任一项目或字元
字串的索引值是从0开始到len(S)-1,另外也可使用负索值的方式取得字串中的字元
ex:取得第一个字元
>>> S[0]'H'

ex:取得最后一个字元
>>> S[len(S)-1]'o'


2012年5月28日

Python-物件型态-数字


Python-物件型态-数字
1.整数四则运算
>>> 123+456579>>> 123-456-333>>> 123*45656088>>> 123/4560除法会只取整数结果

2012年5月21日

利用Python Gmail SMTP 寄送Email


利用Python Gmail SMTP 寄送Email

其中'GmailUserName' 为寄件者Gmail的帐号'GmailPassword'为寄件者Gmail密码'receive@XXX.XXX.XXX'为接收者帐号,一次只能一个,若要寄多个,则必须用成List来依序寄出'subject'寄邮件的标题'testcontent'为邮件的内容程式内容如下:


如何在Windows 环境中将Python 程式(.py)编译成可执行档(.exe)


如何在Windows 环境中将Python 程式(.py)编译成可执行档(.exe)

1.首先必须安装 py2exe-0.6.9.win32-py2.5.exe

2.建立一个setup.py档案,放在你想打包的python程式同一个目录下,程式内容如下,filename.py为你要执行的主程式:from distutils.core import setupimport py2exe
setup(console=['filename.py'])

python 删除目录及其所有档案的方法

python 删除目录及其所有档案的方法

###windows
import os
folder = "C:\\Folder Name"
command = "rmdir /s /q %s"
command = command % folder
result = os.system(command)
if result == 0:
    print "delete success"
else:
    print "delete fail"


python 取得子目录及档案名称的方法

python 取得子目录及档案名称的方法


import glob
strPathToSend='/folder path/'
lstStrFilePath=glob.glob(strPathToSend+'*.*')
lstStrFilePath

2012年5月20日

python 简介

python 简介


本学习手册是以python 2.X为版本介绍,为何是2.X版不是3.X版呢? 主要是为了配合Google Application Engine(GAE)目前GAE支援的python版本为2.X版Google Application Engine开发指南。
Python是一种简单易学,功能强大的直译式程式语言。它具备高层次的资料结构,并且能轻易的透过简单及有效率方式来设计程式的物件导向程式语言。 Python优雅的语法,动态类型,连同它自然语言的解释能力,使它成为最佳的脚本(Script)语言,并且适用于大多数平台上,及可应用于各种领域。




BlogAD