python 的琐碎事

来源:互联网 发布:图书管理系统 java 编辑:程序博客网 时间:2024/05/29 18:46

1、imp 模块提供访问内部import语句的实现机制。

import imp
data = {"class":"re","func":"finditer"}
def call(data):
    file,path,desc = imp.find_module(data["class"])
    mymould = imp.load_module(data["class"],file,path,desc)
    myfunc = mymould.__dict__.get(data["func"])
    return myfunc

str = """<aaa>11111</aaa>"""
match = call(data)(r"<aaa>([\w\s]*)</aaa>",str)
for i in match:
    print i.groups(1)

2、re模块



3、list的sort

students = [{'a':12,'b':34},{'a':1,'b':23},{'a':1,'b':11}];


students.sort(cmp=lambda x,y:cmp(x.get('a'),y.get('a')))
print students;

#推荐
students.sort(key=lambda x:x.get('a'),reverse=True)
print students

#多建值排序
students.sort(key=lambda x:(x.get('a'),x.get('b')))
print students

sorted使用与list的sort类似


4、datetime

import time
from datetime import datetime

now_time_stamp = time.time()
utc = time.gmtime(now_time_stamp)
print now_time_stamp
active_date = datetime.fromtimestamp(now_time_stamp)
print datetime.strftime(active_date, '%Y%m%d--%H:%M')

date_stamp = time.mktime(utc)
date = datetime.fromtimestamp(date_stamp)
print datetime.strftime(date, '%Y%m%d--%H:%M')


原创粉丝点击