码迷,mamicode.com
首页 > 其他好文 > 详细

写过的小程序

时间:2017-01-17 15:25:08      阅读:247      评论:0      收藏:0      [点我收藏+]

标签:sys   print   read   判断   use   随机   end   fill   app   

生成200个10位的随机数(生成优惠券):
__author__ = ‘friday‘
import random

def creat_num(num,long):
    str = ‘qwertyuiopasdfghjklzxcvbnm1234567890!@#$%^&*_+‘
    b = []
    for i in range(num):
        a = ‘‘
        for j in range(long):
            a += random.choice(str)
        b.append(a)
    for i in range(len(b)):
        print(b[i])

if __name__ == ‘__main__‘:
    creat_num(200,10) 

判断有多少个英文单词:
import re
from collections import Counter


def word_count(txt):
    word_pattern = r‘[a-zA-Z-]+‘
    words = re.findall(word_pattern, txt)
    return Counter(words).items()

if __name__ == ‘__main__‘:
    txt = open(‘D:\\f.txt‘, ‘r‘).read().lower()
    print word_count(txt)


为头像加数字:
__author__ = ‘Kxrr‘

from PIL import Image,ImageDraw,ImageFont
import random

msgNum = str(random.randint(1,99))

# Read image
im = Image.open(‘D:\\1.jpg‘)
w,h = im.size
wDraw = 0.8 * w
hDraw = 0.08 * w

# Draw image
font = ImageFont.truetype(‘D:\\1.ttc‘, 30) # use absolute font path to fix ‘IOError: cannot open resource‘
draw = ImageDraw.Draw(im)
draw.text((wDraw,hDraw), msgNum, font=font, fill=(255,33,33))

# Save image
im.save(‘D:\\kxrr_msg.png‘, ‘png‘) 

有个目录,里面是你自己写过的程序,统计一下你写过多少行代码。包括空行和注释,但是要分别列出来。
import os
import re

def find_path(path):
    c = 0
    for i in os.listdir(path):
        py=os.path.join(path,i)
        a=open(py)
        commentline = 0
        blankline = 0
        lines = len(a.readlines())
        print "There are %d lines in %s" % (lines, py)
        for ii in a.readlines():   
            pattern = re.compile(r‘(\s*)#‘)
            pattern1 = re.compile(r‘(\s*)$‘)
            if pattern.match(ii):
                    commentline += 1
            if pattern1.match(ii):
                    blankline += 1
        print "blankline is:",blankline,"commentline is:",commentline

find_path(‘d://1‘)









写过的小程序

标签:sys   print   read   判断   use   随机   end   fill   app   

原文地址:http://www.cnblogs.com/tangbinghaochi/p/6292990.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!