每天学一点,形成一种知识复利
问题33: 生成一个包含大写字母A-Z和数字0-9的随机4位验证码 分析:1. 数字的个数随机产生;2.去除数字后剩下的都是字母;3.随机生成数字;4.随机生成字母;5. 将随机成的数字和字谜结合;6生成最终字符串 demoCode: #!/usr/bin/python3 import random, string def gen_random_string(length): num_of_numeric = random.randint(1,length-1) num_of_letter = length - num_of_numeric numerics = [random.choice(string.digits) for i in range(num_of_numeric)] letters = [random.choice(string.ascii_letters) for i in range(num_of_letter)] all_chars = numerics + letters random.shuffle(all_chars) result = ''.join([i for i in all_chars]) return result if __name__ == '__main__': print(gen_random_string(64)) 问题34: python去除html标签 分析:通过HTMLParser方法进行处理html demoCode: #! /usr/bin/python3 from HTMLParser import HTMLParser class MLStripper(HTMLParser): def __init__(self): self.reset() self.fed = [] def handle_data(self, d): self.fed.append(d) def get_data(self): return ''.join(self.fed) def strip_tags(html): s = MLStripper() s.feed(html) return s.get_data()