python实例练习
来源:互联网 发布:sas编程教程 pdf 编辑:程序博客网 时间:2024/06/05 16:42
1.图片上添加文本
#!/user/bin/env python#coding:utf-8myPath='./media/'fontPath='./media/'inputFile='xwp.jpg'outputFile='output.jpg'import Image , ImageFont , ImageDraw #先安装#打开图片Im=Image.open(myPath+inputFile)draw=ImageDraw.Draw(Im)#根据图片大小确定字体大小fontsize=min(Im.size)/4#加文字font=ImageFont.truetype(fontPath+ 'khmerOS.ttf',fontsize)draw.text((Im.size[0]-fontsize,0),'5',font=font,fill=(256,0,0))Im.save(myPath+outputFile,'jpeg')
2.生成随机数
#!/user/bin/env python#coding:utf-8import string,random#激活码中的字符和数字field=string.letters + string.digits#获得四个字母和数字的随机组合def getRandom(): return "".join(random.sample(field,6))#生成的每个激活码中有几组def concatenate(group): return '-'.join(getRandom() for i in range(group))#生成n组激活码def generate(n): return [concatenate(4) for i in range(n)]if __name__=='__main__': print generate(5)3.单词个数
#!/user/bin/env python#coding:utf-8import refrom collections import CounterFILESOURCE='./test.txt'def getMostCommonWord(articlefilesource): ''' 输入一个英文的纯文本文件,统计其中的单词出现的个数 :param articlefilesource: :return: ''' pattern=r'''[A-Za-z]+|\$?\d+%?$''' with open(articlefilesource) as f: r=re.findall(pattern,f.read()) return Counter(r).most_common()if __name__=='__main__': print getMostCommonWord(FILESOURCE)
阅读全文
0 0
- Python 练习实例1
- Python 练习实例2
- Python 练习实例3
- Python 练习实例4
- Python 练习实例5
- Python 练习实例6
- Python 练习实例7
- Python 练习实例8
- Python 练习实例9
- Python 练习实例10
- ?Python 练习实例11
- Python 练习实例12
- Python 练习实例13
- Python 练习实例14
- Python 练习实例15
- Python 练习实例20
- Python 练习实例18
- Python 练习实例17
- 从0开始做互联网推荐-以58转转为例
- 正则表达式验证Ip地址
- 说说JSON和JSONP,也许你会豁然开朗,含jQuery用例
- 从0开始做垂直O2O个性化推荐-以58到家美甲为例
- JQ中$(window).load和$(document).ready区别与执行顺序
- python实例练习
- 解决打包生成JAR在Android项目中不能调用问题
- 解决Tomcat8“At least one JAR was scanned for TLDs yet contained no TLDs”问题
- Kafka设计解析(一)- Kafka背景及架构介绍
- 58到家入驻微信钱包的技术优化
- Python基础-继承与多态
- Builder模式的简单实现
- SpringBoot 整合 Websocket 实现消息推送框架的设计笔记
- SpringBoot启动报错(Cannot determine embedded database driver class for database type NONE)