在python中生成一个排除某些特殊字符的随机字符串

时间:2019-06-30 08:28:55

标签: python string random

我刚刚开始学习编码。我试图在python中创建密码生成器。研究该网站后,我发现我可以使用

random.SystemRandom().choice(string.punctuation)

我的问题是,如何从标点符号集中生成随机字符(不包括特殊字符'#$~'等)?

s=random.SystemRandom().choice(string.punctuation)

我希望变量s具有随机​​的特殊字符,但不包括我选择的一些字符。

3 个答案:

答案 0 :(得分:1)

对于不使用特殊字符的密码,我想将os.urandombase64.b64encode结合使用:

In [1]: import os                                                                                        

In [2]: import base64                                                                                    

In [3]: base64.b64encode(os.urandom(12), b'__').decode()                                                 
Out[3]: 'Rb1fOnnzO2H4cCYy'

In [4]: base64.b64encode(os.urandom(12), b'__').decode()                                                 
Out[4]: 'sDX8bVqAB7iyf9S1'

唯一的缺点是,根据输入数据的长度,它可以在密码末尾保留'='字符。

我使用以下代码解决该问题:

In [1]: import base64 
   ...: import os                                                                                        

In [2]: def genpw(length): 
   ...:     """ 
   ...:     Generate a random password. 
   ...:  
   ...:     Arguments: 
   ...:         length: Length of the requested password. 
   ...:  
   ...:     Returns: 
   ...:         A password string. 
   ...:     """ 
   ...:     n = roundup(length) 
   ...:     d = os.urandom(n) 
   ...:     return base64.b64encode(d, b'__').decode()[:length] 
   ...:  
   ...:  
   ...: def roundup(characters): 
   ...:     """ 
   ...:     Prevent '=' at the end of base64 encoded strings. 
   ...:  
   ...:     This is done by rounding up the number of characters. 
   ...:  
   ...:     Arguments: 
   ...:         characters: The number of requested (8-bit) characters. 
   ...:  
   ...:     Returns: 
   ...:         The revised number. 
   ...:     """ 
   ...:     bits = characters * 6 
   ...:     upto = 24 
   ...:     rem = bits % upto 
   ...:     if rem: 
   ...:         bits += (upto - rem) 
   ...:     return int(bits / 8) 
   ...:                                                                                                  

In [3]: genpw(7)                                                                                         
Out[3]: 'ctnJF_a'

In [4]: genpw(24)                                                                                        
Out[4]: 'if7EOy8ZR_O7EAXGSwXouCiU'

In [5]: genpw(24)                                                                                        
Out[5]: 'K_6XvVg_zCMRECLayy3oHejg'

In [6]: genpw(24)                                                                                        
Out[6]: 'xEyovBztluUM8XHIoNRRacp1'

您可以在我的github repos之一中以genpw.py的名称找到完整的命令行脚本。

答案 1 :(得分:0)

您可以创建这样的字符串:

characters = "[all the characters you want]"

并使用characters[random.randint(0, len(characters))]

随机选择一个字符

答案 2 :(得分:0)

随机导入 导入字符串 def randomString(stringLength = 10):     “”“生成固定长度的随机字符串”“”     字母= string.ascii_lowercase     返回''.join(ran.choice(letters)for i in range(stringLength))