from string import ascii_uppercase, ascii_lowercase, digits
import random
defget_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
'''ifnot 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 notin d:
d[word] = 1else:
d[word] += 1
wordscount = len(d)
print('words:%d\nlines:%d' % (wordscount, linescount))