mardi 13 mars 2018

Python random password generator randomly not working

I am using the following code for generating some passwords with symbols, numbers and digits.

Sometimes the code works and sometimes not, what is the problem?

import random
import string
import re

template="latex code... placeholder ...other latex code"
latex_passwords=''
for _ in range(30):
    password=''.join(random.choice(string.ascii_lowercase+string.digits+string.ascii_uppercase+string.punctuation) for _ in range(12))
    latex_passwords+=password+'\n'

template=re.sub(r'placeholder',latex_passwords,template)

Additional details: if you run the code 10 times, almost half of the times it will fail giving the following error at the line with re.sub()

  File "/usr/local/Cellar/python3/3.6.4_2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/sre_parse.py", line 887, in addgroup
raise s.error("invalid group reference %d" % index, pos)
sre_constants.error: invalid group reference 8 at position 169




Aucun commentaire:

Enregistrer un commentaire