Python小程序

1.验证码生成器


from string import ascii_uppercase, ascii_lowercase, digits
import random

def get_check_code(len_, mode = (True, True, True)):
    '''check code creater.
    len_ : length of check code.
    mode : tuple type, default (upper=True, lower=True, digit=True).
    example:
    >>> get_check_code(6)
        mCX90t
    >>> get_check_code(4, (False, False, True))
        4820
    >>> get_check_code(4, (True, False, False))
        XMDK
    '''

    if not isinstance(len_, int) or len(mode) != 3:
        raise ValueError
    mode = (True, True, True) if set(mode) == {False} else mode
    chars = ''
    for m, c in zip(mode, (ascii_uppercase, ascii_lowercase, digits)):
        if m:
            chars += c
    return ''.join([random.choice(chars) for i in range(len_)])

if __name__ == '__main__':

    print(get_check_code(4, (True, True, False)))
    print(get_check_code(4, (False, False, True)))
    print(get_check_code(6))



----------

输出:
scFs
2585
npRCAb

统计纯英文文本行数、单词量以及每个单词出现次数

import re 

d = {}
linescount = 0
wordscount = 0
file_text = '/home/duxu/textname.txt'

with open(file_text,'r') as r_file:
    for line in r_file:
        linescount += 1
        words = re.findall(r'[a-zA-Z0-9]+',line)
        for word in words:
            if word not in d:
                d[word] = 1 
            else:
                d[word] += 1
    wordscount = len(d)

print('words:%d\nlines:%d' % (wordscount, linescount))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值