python之生成随机数和字符串
2016-07-04 11:53
471 查看
最近看一个python的教学视频,视频里面有一个文本,一堆帐号,电话号码啥的,于是懒筋犯了,编辑假数据实在费劲,索性让这些数据自动生成
输出结果如下
文件展示如下
END!
#coding:utf-8 import random import sys def stringList(): str1 = [] str3 = [] for i in range(97, 123): str1.append(chr(i)) str2 = ''.join(str1) for i in range(65, 91): str3.append(chr(i)) str4 = ''.join(str3) strall = str2 + str4 return strall n = int(input("Please input u number :").strip()) ''' def idList(): x = 0 x += 1 return x ''' def nameList(): a = random.randint(3,5) name = ''.join(random.sample(stringList(), a)) return name def dePartment(): list1 = ['IT', 'caiwu', 'xiaoshou'] dpt = random.choice(list1) return dpt def phoneList(): num = '0123456789' phone = ''.join(random.choice(num) for i in range(11)) return phone #把所有生成的数据写入到文件 listfile = open('memberlist.txt', 'w') listfile.write('id name department phone\n') listfile.close() output = sys.stdout with open('memberlist.txt', 'a+') as f: sys.stdout = f for i in range(0, n): i += 1 print('%-5d %-8s %-13s %s' % (i, nameList(), dePartment(), phoneList())) sys.stdout = output print('Wrote a total of %d rows of data' % i)
输出结果如下
文件展示如下
END!
相关文章推荐
- Windows下Python机器学习编程环境安装
- 对Python的深度学习库Theano的介绍
- 使用Python学习selenium测试工具-3:unittest库介绍
- python 操作sqlite数据库
- 用pip 安装 python-consul
- 升级Python3
- 我的python学习--第一天
- Python numpy学习
- 机器学习 画出超平面w.T*x+b
- Python实现代码统计工具(终极篇)
- 学python的那些事——初学python之菜鸟的起航
- 特斯拉车祸致死:自动驾驶还不能完全放开方向盘
- 使用Python学习selenium测试工具-2:快速入门
- 使用Python学习selenium测试工具-1:Selenium的前生今世
- Python开发动态网页基础(三)
- 用Python挖掘Twitter数据Part1:数据采集
- 我最喜欢的9个 Python深度学习库
- 在Python中用小波分析图像的哈希值
- Python IDLE快捷键一览
- Python实现代码统计工具——终极加速篇