1 #coding=utf-8 2 import random 3 import string 4 import codecs 5 ''' 6 演示如何使用Python标准库random来生成随机数据,这在需要 7 ''' 8 #常用汉字Unicode编码表 9 StringBase = 'u7684u4e00u4e86u662fu6211u4e0du5728u4ebau4eecu6709u6765u4ed6u8fd9u4e0au7740u4e2au5730u5230u5927u91ccu8bf4u5c31u53bbu5b50u5f97u4e5fu548cu90a3u8981u4e0bu770bu5929u65f6u8fc7u51fau5c0fu4e48u8d77u4f60u90fdu628au597du8fd8u591au6ca1u4e3au53c8u53efu5bb6u5b66u53eau4ee5u4e3bu4f1au6837u5e74u60f3u751fu540cu8001u4e2du5341u4eceu81eau9762u524du5934u9053u5b83u540eu7136u8d70u5f88u50cfu89c1u4e24u7528u5979u56fdu52a8u8fdbu6210u56deu4ec0u8fb9u4f5cu5bf9u5f00u800cu5df1u4e9bu73b0u5c71u6c11u5019u7ecfu53d1u5de5u5411u4e8bu547du7ed9u957fu6c34u51e0u4e49u4e09u58f0u4e8eu9ad8u624bu77e5u7406u773cu5fd7u70b9u5fc3u6218u4e8cu95eeu4f46u8eabu65b9u5b9eu5403u505au53ebu5f53u4f4fu542cu9769u6253u5462u771fu5168u624du56dbu5df2u6240u654cu4e4bu6700u5149u4ea7u60c5u8defu5206u603bu6761u767du8bddu4e1cu5e2du6b21u4eb2u5982u88abu82b1u53e3u653eu513fu5e38u6c14u4e94u7b2cu4f7fu5199u519bu5427u6587u8fd0u518du679cu600eu5b9au8bb8u5febu660eu884cu56e0u522bu98deu5916u6811u7269u6d3bu90e8u95e8u65e0u5f80u8239u671bu65b0u5e26u961fu5148u529bu5b8cu5374u7ad9u4ee3u5458u673au66f4u4e5du60a8u6bcfu98ceu7ea7u8ddfu7b11u554au5b69u4e07u5c11u76f4u610fu591cu6bd4u9636u8fdeu8f66u91cdu4fbfu6597u9a6cu54eau5316u592au6307u53d8u793eu4f3cu58ebu8005u5e72u77f3u6ee1u65e5u51b3u767eu539fu62ffu7fa4u7a76u5404u516du672cu601du89e3u7acbu6cb3u6751u516bu96beu65e9u8bbau5417u6839u5171u8ba9u76f8u7814u4ecau5176u4e66u5750u63a5u5e94u5173u4fe1u89c9u6b65u53cdu5904u8bb0u5c06u5343u627eu4e89u9886u6216u5e08u7ed3u5757u8dd1u8c01u8349u8d8au5b57u52a0u811au7d27u7231u7b49u4e60u9635u6015u6708u9752u534au706bu6cd5u9898u5efau8d76u4f4du5531u6d77u4e03u5973u4efbu4ef6u611fu51c6u5f20u56e2u5c4bu79bbu8272u8138u7247u79d1u5012u775bu5229u4e16u521au4e14u7531u9001u5207u661fu5bfcu665au8868u591fu6574u8ba4u54cdu96eau6d41u672au573au8be5u5e76u5e95u6df1u523bu5e73u4f1fu5fd9u63d0u786eu8fd1u4eaeu8f7bu8bb2u519cu53e4u9ed1u544au754cu62c9u540du5440u571fu6e05u9633u7167u529eu53f2u6539u5386u8f6cu753bu9020u5634u6b64u6cbbu5317u5fc5u670du96e8u7a7fu5185u8bc6u9a8cu4f20u4e1au83dcu722cu7761u5174u5f62u91cfu54b1u89c2u82e6u4f53u4f17u901au51b2u5408u7834u53cbu5ea6u672fu996du516cu65c1u623fu6781u5357u67aau8bfbu6c99u5c81u7ebfu91ceu575au7a7au6536u7b97u81f3u653fu57ceu52b3u843du94b1u7279u56f4u5f1fu80dcu6559u70edu5c55u5305u6b4cu7c7bu6e10u5f3au6570u4e61u547cu6027u97f3u7b54u54e5u9645u65e7u795eu5ea7u7ae0u5e2eu5566u53d7u7cfbu4ee4u8df3u975eu4f55u725bu53d6u5165u5cb8u6562u6389u5ffdu79cdu88c5u9876u6025u6797u505cu606fu53e5u533au8863u822cu62a5u53f6u538bu6162u53d4u80ccu7ec6' 10 StringBase = ''.join(StringBase.split('\u')) #转换为汉字 11 12 def getEmail(): 13 suffix = ['.com','.org','.net','.cn'] #常见域名后缀,可以随意扩展该列表 14 characters = string.ascii_letters + string.digits + '_' 15 username = ''.join((random.choice(characters) for i in range(random.randint(6,12)))) 16 domain = ''.join((random.choice(characters) for i in range(random.randint(3,6)))) 17 return username + '@' + domain + random.choice(suffix) 18 19 def getTelNo(): 20 return ''.join((str(random.randint(0,9)) for i in range(11))) 21 22 def getNameOrAddress(flag): 23 '''flag=1表示返回随机姓名,flag=0表示返回随机地址''' 24 result='' 25 if flag==1: 26 rangestart,rangeend = 2, 5 #大部分中国人的姓名在2-4个汉字 27 elif flag==0: 28 rangestart, rangeend =10, 30 #假设地址在10-30个汉字之间 29 else: 30 print('flag must be 1 or 0') 31 for i in range(random.randint(rangestart,rangeend)): 32 result += random.choice(StringBase) 33 return result 34 35 def getSex(): 36 return random.choice(('男','女')) 37 38 def getAge(): 39 return str(random.randint(18,100)) 40 41 def main(filename): 42 with codecs.open(filename,'w','utf-8') as fp: 43 fp.write('Name,Sex,Age,TelNo,Address,Email ') 44 #quickly generate information of 2000 persons 45 for i in range(20): 46 name = getNameOrAddress(1) 47 sex = getSex() 48 age = getAge() 49 tel = getTelNo() 50 address = getNameOrAddress(0) 51 email = getEmail() 52 line = name+','+sex+','+age+','+''+tel+','+address+','+email+' ' 53 fp.write(line) 54 55 def output(filename): 56 with codecs.open(filename,'r','utf-8') as fp: 57 while True: 58 line = fp.readline() 59 if not line: 60 return 61 line = line.split(',') 62 for i in line: 63 print(i,end=',') 64 print() 65 66 if __name__ == '__main__': 67 filename = 'infomation.txt' 68 main(filename) 69 output(filename) 70 # 输出随机例子 71 # Name,Sex,Age,TelNo,Address,Email 72 # , 73 # 建道围社难,男,90,08827385408,阳任传拿位见野旁系七改而吗唱所界分入像句产赶土七,NbHbp27@PoXTC.cn 74 # , 75 # 背原青火,男,100,69980204333,在日界路其农空五传众色轻,o3Hpo_O@cySL.com 76 # , 77 # 心胜,男,26,39275254854,外导女小古牛记车章强似口用很土长,oRju2LNm4M2e@4h2nM.com 78 # , 79 # 被一,女,86,84246048136,取学东公步其传太子命住动呀相会起青星准路但社结火命放员研风何,v0jkNI@cYbUw.cn 80 # , 81 # 它去阶风,男,60,82598497383,将重月主完四夜报女名会发屋界思张跟算那运行座冲干了业今,zgLxuxMuw@XuM8.com 82 # , 83 # 过根牛渐,女,80,39589625843,停月形岸向紧从今经细现老章五民吧敢快胜北这他该,Sp8FxTLu@3LPj.net 84 # , 85 # 便画系,女,22,97633000098,问最慢产并拿顶们年此才次强界住草要,iTTKSAgxIvs@TPvR2.org 86 # , 87 # 信找群体又,男,28,76565632239,区老背很睡牛该倒两赶当方两强农有长顶,K0BnyJz0tgp@tFHN.org 88 # , 89 # 您政军必,男,39,32755569981,座轻战胜师为好切岁任步钱件合果国,tRDRVoL3@oYC.cn 90 # , 91 # 指十代,男,24,18217496227,老往并队但我脸转术于作这,V1U7oe3FtPf3@beoJ.cn 92 # , 93 # 收敢哥向验,女,57,55710447802,信实数苦公提总风歌半团往通所车该常,5H58Wx@xCGOa.net 94 # , 95 # 性新果非,男,64,80409062009,时林别常做四将脸该石吧开,TUxqK3@BNcnc_.org 96 # , 97 # 问回读,男,79,00311161854,走野都似两有做步和刚火,BZ9ZlCK0a8j@kM8YT.com 98 # , 99 # 识使,男,62,50524583311,觉完睡里学地合条笑五观越时未原要那变,9xFXL2F9o@LTNIJp.com 100 # , 101 # 观系日,男,98,39502402460,究传亲体者雨城去海导志跑手开就内生战便他满万对进加公画,esWBFLLfW9B_@uheFQ.net 102 # , 103 # 飞南,女,30,93888887278,造爱色再去但代头息哥六在背眼哥他十呀这服,lghl04R@sR27.net 104 # , 105 # 起建历先,女,92,61088026843,叔之北要出平结决爬工跟冲法机也再后离九代团让放,ecikIDJgbm@CSYBum.cn 106 # , 107 # 教都我利四,男,43,05229609231,际近紧手通思上然成读钱又,ymMS49op@B5syL.org 108 # , 109 # 着呼读,女,46,57271957224,队深向民作极热少的乡最业记报处通记几似兴,MG2yIa@l6HL.org 110 # , 111 # 孩准火区,男,42,65456625769,重似论位名你您用日很住顶座原阳代被伟工我,NNSKXlWI@_d8kmG.org 112 # ,