Python生成随机字符串

Posted zhzhang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python生成随机字符串相关的知识,希望对你有一定的参考价值。

利用Python生成随机域名等随机字符串。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from random import randrange, choice
from string import ascii_lowercase as lc
from sys import maxsize
from time import ctime

tlds = (com, edu, net, org, gov)

for i in range(randrange(5, 110)):
    dtint = randrange(maxsize/1000)  # pick date
    dtstr = ctime(dtint)        # date string
    llen = randrange(4, 8)      # login is shorter
    login = ‘‘.join(choice(lc) for j in range(llen))
    dlen = randrange(llen, 13)
    dom = ‘‘.join(choice(lc) for j in range(dlen))
    print(%s::%[email protected]%s.%s::%d-%d-%d % (dtstr, login, dom, choice(tlds), dtint, llen, dlen))

该脚本生成拥有三个字段的字符串,由一对冒号或者一对双冒号分隔。第一个字段是随机整数,该整数将被转换为一个日期。下一个字段是一个随机生成的电子邮件地址。最后一个字段是一个由单横线(-)分隔的整数集。

 

输出结果如下

/Library/Frameworks/Python.framework/Versions/3.5/bin/python3.5 /Users/baoshan/Documents/03.python/Python入门经典/python/test.py
Sat Feb  6 23:39:33 18080579::[email protected]::570505799633973-5-10
Fri Aug 28 21:00:12 149488065::[email protected]::4717325545362012-7-8
Tue Aug 29 19:13:28 189633561::[email protected]::5984195035662808-5-5
Sun Mar  9 16:01:45 127723924::[email protected]::4030515577555305-4-12
Sun Feb 24 22:02:17 15157754::[email protected]::478270352959337-4-12
Sat Apr  1 02:16:35 71501341::[email protected]::2256302226478595-7-12
Sun Jul  6 05:02:28 18939664::[email protected]::597615916712548-7-12
Wed Jun 29 17:17:14 114444146::[email protected]::3611446270334234-6-12
Fri May 19 15:08:14 234054828::[email protected]::7385994817398494-4-12
Fri Nov 16 04:22:12 269978536::[email protected]::8519637561999732-7-10
Fri Jul  8 08:51:02 132470935::[email protected]::4180316786239862-5-7
Wed Aug 10 16:48:49 36911560::[email protected]::1164754179132529-6-7
Sat Nov 29 23:20:26 149477394::[email protected]::4716988809146426-6-10
Fri Jul  7 13:19:11 152052986::[email protected]::4798266629692751-4-10
Fri Nov 20 18:10:08 184687953::[email protected]::5828126728558208-7-8
Thu May 14 20:58:39 74067665::[email protected]::2337287593582719-6-9
Wed Dec  4 16:20:16 6375405::[email protected]::201126211518016-6-8
Tue Oct  2 21:39:46 105796046::[email protected]::3338538601988386-6-8
Wed Oct 29 01:21:40 242249941::[email protected]::7644607618929700-4-12
Fri Oct 28 14:23:42 225762907::[email protected]::7124327078307822-4-8
Fri May 17 05:26:04 237385793::[email protected]::7491109919808364-6-8
Sat Nov  9 06:58:43 32473963::[email protected]::1024717151372323-6-7
Tue Jan 29 11:24:31 48403935::[email protected]::1527418488597871-5-6
Sat Jul 16 01:41:29 63586450::[email protected]::2006532400268489-5-11
Tue Jun 17 15:53:16 125570492::[email protected]::3962559836044396-6-10
Fri Jul  6 18:02:18 110124485::[email protected]::3475130936176938-6-11
Mon Nov  6 02:59:19 128389967::[email protected]::4051533885361159-5-11
Wed Aug 29 07:26:21 231020587::[email protected]::7290243428541981-4-7
Wed Feb 12 04:46:47 44084358::[email protected]::1391105805770807-4-6
Thu Mar  6 21:35:05 269774380::[email protected]::8513194998922505-6-11
Sun Aug  1 04:50:36 5675115::[email protected]::179027182702236-7-10
Fri Jan 30 11:55:08 173445807::[email protected]::5473358841383708-5-11
Mon Nov 23 15:06:56 92711344::[email protected]::2925625293529616-5-7
Fri Sep  1 18:57:03 122804541::[email protected]::3875274859604223-6-9
Thu Aug 19 06:30:59 225684934::[email protected]::7121866482052259-5-11
Tue Jan 13 23:40:47 220538415::[email protected]::6959458010216447-5-10
Mon Jan 29 19:35:42 156070638::[email protected]::4925051467241742-7-7
Tue Jun  9 14:24:17 10843671::[email protected]::342131051859857-7-8
Mon Oct 21 00:17:29 28336307::[email protected]::894145337885849-5-11
Wed Aug  7 11:14:04 290314458::[email protected]::9161377267720444-6-11
Sun Nov  4 20:56:12 249269523::[email protected]::7866124231697772-7-10
Sun Oct 31 16:30:24 151044100::[email protected]::4766429272638624-5-5
Tue Mar 22 17:36:01 205694935::[email protected]::6491043030188161-6-12
Sat Mar  1 10:14:01 99822200::[email protected]::3150022211864041-7-11
Mon Jan  8 03:56:40 58937049::[email protected]::1859811459739000-7-8
Tue Jul  3 23:50:07 141736610::[email protected]::4472713247068207-4-6
Wed Apr  5 02:00:40 211115786::[email protected]::6662108566202440-7-7
Sat Mar  6 15:37:16 227311288::[email protected]::7173189242955436-4-8
Wed Mar 10 00:24:24 220025756::[email protected]::6943280059585464-5-12
Wed Jun 21 17:55:56 164719426::[email protected]::5197980867357356-4-7
Fri Sep 14 08:34:01 219967392::[email protected]::6941438285934841-5-12
Fri Feb 15 10:49:01 200550532::[email protected]::6328701348576541-5-7
Tue Nov  4 23:24:52 141649519::[email protected]::4469964931236292-5-9
Thu May 26 12:30:05 82025836::[email protected]::2588423214803405-7-12
Wed Dec 23 18:00:32 127106522::[email protected]::4011032277252032-4-6
Mon Feb  1 03:20:18 25237909::[email protected]::796369318341618-6-9
Tue Nov 21 08:08:53 93402215::[email protected]::2947427076211733-5-9
Thu Jul 27 19:23:06 134687724::[email protected]::4250271892044186-5-5
Mon Mar 14 23:19:16 228798687::[email protected]::7220127022413556-4-11
Wed Mar 31 03:42:05 209315677::[email protected]::6605302612506125-4-12
Wed Nov  8 00:01:06 236681578::[email protected]::7468887055881666-7-7

Process finished with exit code 0

 

上述参考自:Python核心编程

 

谢谢

以上是关于Python生成随机字符串的主要内容,如果未能解决你的问题,请参考以下文章

python生成随机长度字符串并写入文件

python生成随机长度字符串并写入文件

生成随机码

Python随机生成指定长度字符串并保存到mysql中

python 生成随机字符串

python生成随机数随机字符串