简体   繁体   中英

Generate a random string excluding certain special characters in python

I just started learning coding. I was trying to create a password generator in python. After researching this website, I found that I can use,

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

My question is, How can I generate random characters from the set of punctuation excluding the special characters '#$~' etc?

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

I would like my variable s to have random special characters excluding a few of my choice.

For a password that does not use special characters, I like to combine os.urandom with base64.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'

The only downside is that it can leave '=' characters at the end of the password depending on the length of the input data.

I use the following code to get around that:

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'

You can find the complete command-line script in one of my github repos under the name genpw.py .

You could create a string like this:

characters = "[all the characters you want]"

and just pick a random character with characters[random.randint(0, len(characters))]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM