Chinaunix首页 | 论坛 | 博客
  • 博客访问: 177010
  • 博文数量: 36
  • 博客积分: 2510
  • 博客等级: 少校
  • 技术积分: 410
  • 用 户 组: 普通用户
  • 注册时间: 2009-04-04 12:39
文章分类

全部博文(36)

文章存档

2010年(1)

2009年(35)

我的朋友

分类: LINUX

2009-10-25 15:10:47

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,
 
阅读(649) | 评论(1) | 转发(0) |
0

上一篇:两台linux间互相SSH

下一篇:linux配置SMB共享

给主人留下些什么吧!~~

chinaunix网友2010-01-22 13:13:07

List有很多的操作 : append( x) 在List的末尾添加一个成员;相当于a[len(a):] = [x]. extend( L) 把一个List添加到当前List的末尾;相当于a[len(a):] = L. insert( i, x) 在指定位置插入一个成员。X会插在第i个成员的前面。所以a.insert(0, x) 插在List的最前面,a.insert(len(a), x) 相当于a.append(x). remove( x) 从List里删掉成员X 。如果没有这个成员的话会报错。 pop( [i]) 删除指定位置的成员,并且返回这个成员。如果没有指定index,a.pop() 会返回list的最后一个成员,并把它从list里删除。 index( x) 返回在list里值为X的对象的的索引,如果没有会报错。 count( x) 返回x在list里出现的次数。