我被困在这行代码中,试图制作一个文本生成器,生成带有词汇表的随机文本,就像我试图生成一个代码,该代码以大写字母ABCDE和小写字母ABCDE以及数字12345来生成文本,这是我被困在上面的代码:
import string
import random # define the random module
S = 681 # number of characters in the string
#call random.choices() string module to find the string in Uppercase + numeric data.
ran = ''.join(random.choices(string.ascii_uppercase + string.digits + string.ascii_lowercase, k = S))
print("Generated text is : " + str(ran)) # print the random data如果你能帮忙,我会非常感激(我是西班牙人,所以如果我英语说得不好,那就是为什么)
发布于 2022-02-06 15:58:47
把你想要的信放在那里就行了。
import string
import random # define the random module
S = 681 # number of characters in the string
letters = 'abcdefg'
ran = ''.join(random.choices(letters, k = S))
print("Generated text is : " + str(ran)) # print the random data发布于 2022-02-06 15:59:41
我猜您希望只生成一个字符为[A, B, C, D, E, a, b, c, d, e, 1, 2, 3, 4, 5]的字符串。
您可以轻松地使用一组受约束的字符来使用:
ran = ''.join(random.choices("ABCDEabcde12345", k = S))K=30的输出:
>>> ''.join(random.choices("ABCDEabcde12345", k=30))
'aCdEaaeC5C52eBaD1d1AC543Ace55c'https://stackoverflow.com/questions/71008767
复制相似问题