posted @ 2004-08-09 02:53 James 阅读(1108) 评论(2) 编辑

python种的标准库:

sys
sys.argv所有的参数。
import sys
if '-h' in sys.argv: print 'this is help\n'

sys.exit( exitcode), sys.exit(0)

sys.stdin, data=sys.stdin.readlines()
sys.stdout,
sys.stderr
sys.platform
sys.path,返回list

type
if type('abc') == types.StringType: (true)
if type('abc') == type( '' ) (true)

copy
copy.copy()浅copy
copy.deepcopy()深copy

string
string.split()
>>> import string
>>> string.split("a b c")
['a', 'b', 'c']
string.atoi(str,base)
string.atof(str)
string.atol(str,base)
string.upper()
string.lower()
string.find()找位置
string.join()合并
string.lstrip(), string.rstrip(), string.strip()
string.replace()

re正则表达式

os
os.environ['PATH'] ='/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/sbin/:.'
os.name  = 'posix'
os.getcwd()
os.listdir('/'),列出所有的dir
os.rename(oldfile, newfile)
os.chmod()
os.system('cmd'),执行命令
os.remove()
os.mkdir()
os.rmdir()
os.removedirs(),删除所有
os.path.exists(),是否存在?
os.path.isfile(),os.path.isdir()
os.path.split()
>>> os.path.split('/home/james/.vimrc')
('/home/james', '.vimrc')

time
time.time(),由Unix纪元(1970年首)到现在的秒数,UTC
time.localtime() = (2004, 8, 8, 23, 19, 53, 6, 221, 1), 年,月,日,小时,分钟,秒,周几,今年的第几天,夏时制
time.asctime() = 'Sun Aug  8 23:20:03 2004'
time.sleep()中断,秒数,象C#种的thread.sleep()

tempfile
tempfile.mktemp()产生一个临时文件


其他的库(日后研究)
mutex
signal
socket
thread
threading
zlib
gzip
urllib
httplib
ftplib
poplib
nntplib
smtplib
telnetlib
urlparse
socketserver
htmllib
xmllib
rfc82lib
base64
xdrlib(RPC)
md5
sha
posix
crypt

posted @ 2004-08-09 02:09 James 阅读(783) 评论(0) 编辑
1,一些内置函数:
coerce 把 两个变元变成同一种类型。
>>> coerce(1,2.2)
(1.0, 2.2000000000000002)

filter, filter(function, list) (2)里面已经说了。

input()输入一个数字
>>> a=input("age: ")
age: 20
>>> a
20

open(),打开文件,open(filename,mode)
pow(x,y), pow(2,10) = 1024
raw_input( prompt ) 输入字符串
reduce(fun,list)
>>> import operator
>>> reduce(operator.add,[1,23])
24

range,xrange, xrange不会引起内存不足。
len()返回长度
max(1,2,3) = 3
min(1,2,3) =1
setattr(object, name, value)
getattr(object,name)
hasattr(object,name)
delattr(object,name)
type(object), type( '' ) = <type 'str'>
dir()列出可用的语法,比如: import os : dir(os)
callable(object),是否可以调用

abs()
cmp(x,y) , x > y 返回 1, x=y 返回 0, x<y, 返回 -1
round(num, dec),  round(10.45,1 ) = 10.5, dec是保留的小数的位数。
eval( '2*3+ 1' ) = 7
exec,执行python语句,
>>> a='print "hello"'
>>> exec a
hello
execfile(file),执行file里面的python

chr(integer), chr(65)='A',
ord('A') = 65
hex(),转换为16进制。 hex(12) = 0xc

2,文件操作
read(), read(nbytes),如果带有数字,那么读出nbytes个字节,否则全部读出。
readline()读出一行
readlines()读出所有的行,作为list返回。
write(),写入字符串。
writelines(list),写入多行
seek()转向文件的位置,0表示开头,1表示当前,2表示尾部。
tell(),返回当前指针。
flush()
close()

posted @ 2004-08-09 01:22 James 阅读(535) 评论(0) 编辑

1,Python中的数据类型。
NoneType, TypeType(自定义类型), IntType, LongType, FloatType, ComplexType(复数), StringType, UnicodeType,TupleType, ListType, DictType, FunctionType

LongType在python中是没有长度限制的,这个也是script的优点。

2,filter(), map(), reduce()
filter(function, list) return list. 筛选规律是 function = true Example:
>>>def f(x): return x %2 != 0
...
filter(f, range(2,10) ), 结果是3,5,7,9

map(function, list), return list, 规律是对list进行运算。
>>> def f(x): return x*x
...
>>> map(f,range(1,10))
[1, 4, 9, 16, 25, 36, 49, 64, 81]

reduce类似进行累加,
>>> def add(x,y): return x+y
...
>>> reduce(add,range(1,11))
55

3,用del来删除list中的元素:
>>> a=[0,1,2,3,4]
>>> del a[0]
>>> a
[1, 2, 3, 4]
>>> del a[1:3]
>>> a
[1, 4]

4,if ... elif ...else 结构。不是elseif啊。

5,raw_input用来得到用户的输入,象Console.ReadLine()
>>> a=raw_input("Your name: ")
Your name: hacker.net
>>> a
'hacker.net'

6, while 循环:
while <条件>:
    语句(注意缩进)

7, for 循环:
 for var in <list or string>:
   语句(缩进)
>>> for str in "hello,world":
...     print str

>>> l=['a','b','c']
>>> for s in l:
...     print s,
...
a b c


8, try, except:
 try:
   语句
except:
  语句

9,range函数:返回一个list
range(10) = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
range(5,10) = [5, 6, 7, 8, 9]
range(1,10,2) =[1, 3, 5, 7, 9]
range(10,1,-1) = [10, 9, 8, 7, 6, 5, 4, 3, 2]
经常用的遍历方法,构造一个range
>>> a=['a','b','c']
>>> for i in range(len(a)):
...     print a[i],
...
a b c

posted @ 2004-08-09 00:28 James 阅读(579) 评论(0) 编辑

这周有一个项目要用python来写,没有办法,只好学习这个东西了。比起C#+VS.Net这种黄金组合来,python+vi用起来还是非常不舒服,但是工作还是比兴趣更重要,不喜欢也要硬学。

首先感觉python在总体概念上和C#有很多相似的地方,比如也要生产中间byte code.

1,奇怪的运算符号: divmod
>>> (a, b) = divmod(10,3)
>>>print a, b 就是 3 1

2,逻辑表达式:
和C一样,没有boolean类型,false可以用下面的东西来表示: None, 0, 0.0, '',"",[],(),{}来表示。

3,语法以缩进来区分,要特别小心。

4, list, 和C#中的arraylist差不多。用[]表示
a=[ 'a', 'b' ],那么a[0]='a', a[1]='b',用len(a)可以得到list的长度,2。

5,子list的提取,可谓花样繁多。
a=[1,2,3,4,5]
a[1,4] = [2,3,4] 实际上就是a[1],a[2],a[3],注意,不包括后面的a[4].
a[-1]=5, -表示从后面取。
a[:]是全部
a[:3]是a[0],a[1],a[2]
a[3:]是从a[3]后面的所有的,就是a[3],a[4]
a[1:-1] 就是a[1],a[2],a[3], 不包括a[-1](即a[4])

6,处理list的方法
a=[0,1,2,3,4,5]
a.append(6),那么a=[0,1,2,3,4,5,6]
a.acount(1) = 1,用来计算1在这个list中出现的次数。
len(a)是长度,这时候=7
a.extend 还可以把list当参数,比如a.extend([7,8])
a.index(6)返回6在这个list中的位置,=6,如果你a.index(10),就会抛出一个异常,我也不知道为什么象其他语言一样,返回-1,这个就是我以前说的程序语言中的方言。
a.insert(0, -1)在0的位置插入-1,这时候a=[-1, 0, 1, 2, 3, 4, 5, 6, 7, 8]
a.pop(),删除并返回最后一个元素。 a.pop()=8,这时候a=[-1, 0, 1, 2, 3, 4, 5, 6, 7]
a.pop(1),可以pop出a[1]。a.pop(0)是pop出a[0]
a.remove(7),找到第一个7,然后删除这个找到的7,这时候a=[-1, 0, 1, 2, 3, 4, 5, 6]
a.reverse(),顺序颠倒,这时候a=[6, 5, 4, 3, 2, 1, 0, -1]
a.sort()排序,这时候 a 会以大小排序,a=[-1, 0, 1, 2, 3, 4, 5, 6]
a.sort(fun)象C#一样,也可以排序函数。
就这些操作函数了,如果你象知道所有的对a的操作函数,dir(a)会给你列出来。比如这里等于:
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__repr__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__str__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

7,针对list的一个新奇的循环
[ express1 for k in list if express2]
比如: a=[1,2,'a']
[k+1 for k in a if type(k)==types.IntType ] 会得到[2,3],当然首先要import  types

8,字符串
"""就是HERE Document.比如
"""
line1
line 2
"""

特殊符号。\u{xxxx} unicode, \xhh,十六进制, \0 8进制。

针对字符串的转换:
float(str),转换为float, a='2.3', float(a) = 2.3
int(str), 转换为int,注意必须要能转换为int, a='2.3', int(a)会出错。a='2.0', int(a)也出错。a='2'就不出错了
int(str, base)根据进制转。 a=‘15’, int(a,8) = 13,意思是把8进制的15转换为10进制的13,最后结果一定是10进制。
long(str), long(str,base)转换为long型。

字符串操作:
连接: +, 'hello' + ' ' + 'world'
capitalize()首字母变成大写. 'hello world'.capitalize() = 'Hello world'
lower()变成小写,upper()变成大写, swapcase()大小写互换。
len()得到字符串长度,a='abc', a[0]='a', a[-1]='c'

判断字符串的类别:
str.isalnum(), 匹配0-9,a-zA-Z
str.isalpha(), a-zA-Z
str.isdigit(), 0-9
str.islower(), a-z
str.isupper(), A-Z
str.istitle(),首字母大写
str.isspace(),空白字符

字符串查找:
a.find('a')找'a'的位置,找不到返回-1
a.find('a', 1)从a[1]开始找
a.find('a', 1, 3),从a[1]到a[3]的这段找。
a.rfind()从后面找
s.index()和find()一样,找不到会返回异常
s.rindex()从后面找
s.count('a'),出现的次数。

字符串合并和分解:
str.join(words), s.split(' ')
'\n'.join(['a','b','c']) = 'a\nb\nc'
'have a good day'.split(' ') = ['have', 'a', 'good', 'day']

posted @ 2004-08-08 19:40 James 阅读(848) 评论(1) 编辑