Python 基本使用

遍历存元组的列表进行操作

一堆xy坐标,找出x,y的左上角与右下角
  1. max_t = reduce(lambda x,y: (max(x[0],y[0]),max(x[1], y[1])),l)  
  2. p;   min_t = reduce(lambda x,y: (min(x[0],y[0]),min(x[1], y[1])),l)  



用zipfile写压缩包

  1. f = zipfile.ZipFile("testzip.zip","w",zipfile.ZIP_DEFLATED)  
  2. f.write("e:/a/1.max","1.max");  
  3. f.write("e:/a/2.max","2.max");  
  4. f.close()  
  5. print "over"  



往sqlite3写入blob类型的数据

blob对应于python的buffer,具体看例子:
  1. #coding:utf8  
  2. import sqlite3  
  3.   
  4. create_sql = "create table if not exists test_blob('id' integer primary key AUTOINCREMENT, 'b' BLOB);"  
  5. db = sqlite3.connect("test.db")  
  6. cur = db.cursor()  
  7. cur.execute(create_sql)  
  8. #写二进制文件  
  9. f = open("CE6.jpg","rb")  
  10. data = f.read()  
  11. f.close()  
  12. cur.execute("insert into test_blob('b') values(?)",(buffer(data),))#使用buffer,跟sqlite3.Binary()效果一样  
  13. db.commit()  
  14. #读文件  
  15. cur.execute("select b from test_blob")  
  16. b = cur.fetchone()[0]  
  17. with open('test2.jpg','wb') as f:  
  18.     f.write(b)  
  19. db.commit()  
  20. db.close()  


获取随机字符串:

  1. "".join(random.sample('abcdefghijklmnopqrstuvwxyz',10))  

复制文件夹,

以前喜欢自己写,才发现有现成的

  1. shutil.copytree("1234","new_1234")  

获取随机的uuid

没有什么技巧
  1. uuid.uuid4()  

原文地址:https://www.cnblogs.com/UnGeek/p/2787977.html