• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

如何在输出之间自动添加破折号?

python 来源:Stopit Donk 5次浏览
import random 
import string 

oneFile = open('password.txt', 'w') 
userInput = 0 
key_count = 0 
key = [] 
chars = string.ascii_uppercase + string.digits + string.ascii_lowercase 

for userInput in range(int(input('How many keys needed?'))): 
    while key_count <= userInput: 
     number = random.randint(1, 999) 
     if number not in key: 
      key_count += 1 
      key.append(number) 
      text = str(number) + ": " + str(''.join(random.sample(chars*6, 16))) 
      oneFile.write(text + "\n") 

oneFile.close() 

print("Data written, please Rename or it will be over written.") 
raw_input("press enter to exit") 

我如何得到它让出来放看起来是这样的:
955:PFtKg-r1fd1-g9FX23与字符的选择量后的破折号之间?如何在输出之间自动添加破折号?

text = str(number) + ": " + str(''.join(random.sample(chars*6, 16))) 
#puts everything together but i would have to repeat 
# + str(''.join(random.sample(chars*6, 16))) on the line in code 

===========解决方案如下:

基于this你可以写(dashPer代表字符选定量):

'-'.join(text[i:i+dashPer] for i in range(0, len(text), dashPer)) 

在刚刚替换:

text = str(number) + ": " + str(''.join(random.sample(chars*6, 16))) 

有:

dashPer = 5 
text = str(''.join(random.sample(chars*6, 16))) 
text = '' + '-'.join(text[i:i+dashPer] for i in range(0, len(text), dashPer)) 
text = str(number) + ": " + text 

版权声明:本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。
喜欢 (0)