1. file operation
fp = open("filename")
line = fp.readline() //get line
content = fp.read() //all content of file, that is, read to end
2. number
8/9 = 0
8/float(9) = 0.88888
3. sort list
list=['a','e','b','c']
sorted(list) // sort list
len(list) //length of list
for item in list:
print item //access each item in list
4. sort dict
dict={'a':2,'e':4,'b':2}
sorted_keys = sorted(dict.keys())
for key in sorted_keys:
print dict[key] //first sort the key, then retrive value for each sorted key
for k,v in dict.iteritems():
print k, v // access every key and value
5. string
import string
range(start, end, step ) // end will not be contained
range( 3, 1, -1 ) => 3 2
int('2') //string to int
str(2) //int to string
'a'.isdigit() False
6. string format
%-10s string type, left-pagged, 10 placehode
print "%-10s %-20s" % ("sss", "tt")
"sfsd".find('f') == 1
"aab".replace("b","") == "aa"
7. datetime
from datetime import datetime
datetime.max //max datetime
datetime.min //mindatetime
datetime.strptime("Sun Oct 25 00:15:34 2009","%a %b %d %H:%M:%S %Y")
datetime.weekday() //with ()
datetime.month
datetime.day
datetime.hour
datetime.minute
datetime.second
timedelta = datetime1 - datetime2
all kinds of datetime format can be found:
http://blog.csdn.net/suiyunonghen/archive/2009/03/19/3999986.aspx
8. json parser
import simplejson
result = simplejson.loads(content) //retrun dict{} or list[] accoring content,
阅读(679) | 评论(1) | 转发(0) |