简体   繁体   English

随机密码生成器随机不起作用

[英]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() 其他详细信息:如果您运行代码10次,几乎有一半的时间它会失败,在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

re.sub interprets sequences of \\ number in the replacement as references to groups in the pattern (among other escape sequences) . re.sub将替换中的\\ number序列解释为对模式中的组的引用(以及其他转义序列) You can use a plain string replacement instead: 您可以使用普通字符串替换:

template = template.replace('placeholder', latex_passwords)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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