遍历存元组的列表进行操作
一堆xy坐标,找出x,y的左上角与右下角
- max_t = reduce(lambda x,y: (max(x[0],y[0]),max(x[1], y[1])),l)
- p; min_t = reduce(lambda x,y: (min(x[0],y[0]),min(x[1], y[1])),l)
用zipfile写压缩包
- f = zipfile.ZipFile("testzip.zip","w",zipfile.ZIP_DEFLATED)
- f.write("e:/a/1.max","1.max");
- f.write("e:/a/2.max","2.max");
- f.close()
- print "over"
往sqlite3写入blob类型的数据
blob对应于python的buffer,具体看例子:
- #coding:utf8
- import sqlite3
- create_sql = "create table if not exists test_blob('id' integer primary key AUTOINCREMENT, 'b' BLOB);"
- db = sqlite3.connect("test.db")
- cur = db.cursor()
- cur.execute(create_sql)
- #写二进制文件
- f = open("CE6.jpg","rb")
- data = f.read()
- f.close()
- cur.execute("insert into test_blob('b') values(?)",(buffer(data),))#使用buffer,跟sqlite3.Binary()效果一样
- db.commit()
- #读文件
- cur.execute("select b from test_blob")
- b = cur.fetchone()[0]
- with open('test2.jpg','wb') as f:
- f.write(b)
- db.commit()
- db.close()
获取随机字符串:
- "".join(random.sample('abcdefghijklmnopqrstuvwxyz',10))
复制文件夹,
以前喜欢自己写,才发现有现成的
- shutil.copytree("1234","new_1234")
获取随机的uuid
没有什么技巧
- uuid.uuid4()