需求:生成随机不重复验证码。

代码:

#!/usr/bin/env python
# encoding: utf-8
"""
@author: 侠之大者kamil
@file: 200number.py
@time: 2016/4/13 23:33
"""
import random,string
def rand_str(num,length = 7):
f = open("Activation_code2.txt","wb")
for i in range(num):
chars = string.ascii_letters + string.digits
s = [random.choice(chars) for i in range(length)]
f.write(bytes((''.join(s) + '\n' ), 'utf-8')) #f.write(''.join(s) + '\n') py2
f.close()
if __name__=="__main__":
rand_str(200)

会逐行写在文件里,涉及知识点:f.open  f.writer random

#’str’ does not support the buffer interface  在python3 报错
with open("test.txt") as fp:
line = fp.readline()
with open("test.out", 'wb') as fp:
fp.write(line) #解决方案1 在Python3x中需要将str编码,
with open("test.txt") as fp:
line = fp.readline()
with open("test.out", 'wb') as fp:
fp.write(bytes(line, 'utf-8'))
#解决方案2 不想用b(binary)模式写入,那么用t(text, 此为写入的默认模式)模式写入可以避免这个错误.
with open("test.txt") as fp:
line = fp.readline()
with open("test.out", 'wt') as fp:
# with open("test.out", 'w') as fp:
fp.write(line)

随机验证码生成(python实现)的更多相关文章

  1. Python教程:随机验证码生成和join 字符串

    函数:string.join() Python中有join()和os.path.join()两个函数,具体作用如下: join(): 连接字符串数组.将字符串.元组.列表中的元素以指定的字符(分隔符) ...

  2. 随机验证码生成和join 字符串

    函数:string.join() Python中有join()和os.path.join()两个函数,具体作用如下: join(): 连接字符串数组.将字符串.元组.列表中的元素以指定的字符(分隔符) ...

  3. PYTHON 随机验证码生成

    # 生成一个六位随机验证码 import random # random 生成随机数 temp = '' for i in range(6): num = random.randrange(0,6) ...

  4. python-内置函数-callable,chr,ord,bytes,随机验证码生成

    s="老男人" bytes(s,encoding="utf-8") 随机验证码的实现方法: 大写字母: li = [] for i in range(6): t ...

  5. C#系统登录随机验证码生成及其调用方法

    话不多说,直接上代码 public ValidateCode() { } /// <summary> /// 验证码的最大长度 /// </summary> public in ...

  6. python笔记-20 django进阶 (model与form、modelform对比,三种ajax方式的对比,随机验证码,kindeditor)

    一.model深入 1.model的功能 1.1 创建数据库表 1.2 操作数据库表 1.3 数据库的增删改查操作 2.创建数据库表的单表操作 2.1 定义表对象 class xxx(models.M ...

  7. Python 生成随机验证码

    Python生成随机验证码  Python生成随机验证码,需要使用PIL模块. 安装: 1 pip3 install pillow 基本使用 1. 创建图片 1 2 3 4 5 6 7 8 9 fro ...

  8. Python生成随机验证码

    Python生成随机验证码,需要使用PIL模块. 安装: pip3 install pillow 基本使用 1.创建图片 from PIL import Image img = Image.new(m ...

  9. python模块之PIL模块(生成随机验证码图片)

    PIL简介 什么是PIL PIL:是Python Image Library的缩写,图像处理的模块.主要的类包括Image,ImageFont,ImageDraw,ImageFilter PIL的导入 ...

随机推荐

  1. BZOJ 1304: [CQOI2009]叶子的染色

    1304: [CQOI2009]叶子的染色 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 566  Solved: 358[Submit][Statu ...

  2. CF#345 (Div1)

    论蒟蒻如何被cf虐 以下是身败名裂后的题解菌=========== Div1 A.Watchmen 有n个点,每个点有一个坐标.求曼哈顿距离=欧几里得距离的点对数量. 只需要统计x或y一样的点对数量. ...

  3. SQL Server 用SSMS查看依赖关系有时候不准确,改用代码查

    SQL Server 用SSMS查看依赖关系有时候不准确,明明某个sp中有用到表tohen,查看表tohen的依赖关系的时候,却看不到这个sp 用代码查看方式如下: --依赖于表tohen的对象 SE ...

  4. asp中的md5/sha1/sha256算法收集

    对于asp这种古董级的技术,这年头想找一些有用的资料已经不容易了,下面是一些常用的加密算法: md5 (将以下代码另存为md5.inc) <% Private Const BITS_TO_A_B ...

  5. hadoop家族之pig入门

    昨天成功运行第一个在hadoop集群上面的python版本的wordcount,今天白天继续看网上提供的文档.下午上头给定的回复是把hadoop家族都熟悉一下,那就恭敬不如从命,开始学习pig吧- 这 ...

  6. Qt5 新特性

    Qt 5 已经临近发布,其最大的特点就是模块化.将原来庞大的模块更细分为不同的部分,同时,一个大版本的升级,当然少不了添加.删除各个功能类.文本简单介绍 Qt5 的特性,其具体内容来自 Qt5 官方 ...

  7. Qt学习笔记 TableWidget使用说明和增删改操作的实现

    看一下效果很简单的一个小功能 先说分部讲一下过程 再给出详细代码 添加数据 MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ...

  8. SQL Server 2012新特性(1)T-SQL操作FileTable目录实例

    在SQL Server 2008提供FileStream,以借助Windows系统本身的API来强化SQL Server对于非结构化数据的支持后,SQL Server 2012更是推出了像Contai ...

  9. C#访问Azure的资源

    官方参考资料在这里:https://msdn.microsoft.com/en-us/library/azure/dn722415.aspx,本文放一些重点及遇到的坑的解决办法. 身份验证 不是说,我 ...

  10. js如何判断一个数组

    typeof [] 为一个"object" 不能通过此方法判断一个数组 方法 1.instanceof方法,这个方法用的比较多. 2.这个是es5以后推荐的方法,Object.pr ...