Skip to content

Instantly share code, notes, and snippets.

@ImSingee
Last active May 31, 2018 09:01
Show Gist options
  • Save ImSingee/2a02b5ed037c61f4d6ba174fb6cb1ffe to your computer and use it in GitHub Desktop.
Save ImSingee/2a02b5ed037c61f4d6ba174fb6cb1ffe to your computer and use it in GitHub Desktop.
获取定长随机字符串;可自定义分割方式
SEEDS = (
(0, '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'),
(1, '1234567890'),
(2, '1234567890abcdefghijklmnopqrstuvwxyz'),
(3, '1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ'),
(4, 'abcdefghijklmnopqrstuvwxyz'),
(5, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'),
(6, '!@\\#$%^&*()/_+=-\'\"'), # 特殊符号:!@\#$%^&*()/_+=-'"
(7, ''),
(8, ''),
)
def get_random_str(length=8, seed=None, sep=None):
import random
if seed is None:
seed = '1234567890abcdefghijklmnopqrstuvwxyz' # 默认:数字+小写字母
sa = []
for i in range(length):
sa.append(random.choice(seed))
result = ''.join([random.choice(seed) for _ in range(length)])
print(result)
# sep格式:((nA, sA), (nB, sB), (nC, sC))
if sep is not None:
r = result
result = ''
l = 0
for ss in sep:
if type(ss) is tuple and len(ss) > 1:
n, s = ss
else:
n = int(ss)
s = ''
result += r[l:l + n] + s
l += n
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment