python小应用合集(有新的会一直更新,欢迎回复补充)

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

一堆xy坐标,找出x,y的左上角与右下角
    max_t = reduce(lambda x,y: (max(x[0],y[0]),max(x[1], y[1])),l)
    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()


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值