gpt4 book ai didi

Python随 secret 码生成器随机不起作用

转载 作者:行者123 更新时间:2023-11-30 22:18:47 24 4
gpt4 key购买 nike

我使用以下代码生成一些包含符号、数字和数字的密码。

代码有时能用,有时不能,问题出在哪里?

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)

其他详细信息:如果运行代码 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解释 \<i>number</i> 的序列在替换中作为对模式 (among other escape sequences) 中的组的引用。您可以使用纯字符串替换:

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

关于Python随 secret 码生成器随机不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49267157/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com