我现在正在学习python,所以我还是新手。我已经建立了一个加密系统,可以将初始密码,加密密码和日期输出到文本文件中。现在的方法非常简单:
now = datetime.now()
date = ('%s/%s/%s %s:%s:%s' % (now.day, now.month, now.year, now.hour, now.minute, now.second))
writePass = finalPass.translate(passSwap)
with open("capec_dictionary.txt", "a") as text_file:
print(initialPass.format(), " - ", writePass.format(), " - ", date, file=text_file)
print("Password has been saved to dictionary.")
结果如下所示:
hi - ******* - 18/9/2015 17:55:15
hello - ********** - 18/9/2015 17:55:20
test - ********** - 18/9/2015 17:55:23
testing - ********** - 18/9/2015 17:55:28
password - ************* - 18/9/2015 17:55:34
passwords - ************** - 18/9/2015 17:55:45
myspecialpassword - ************************ - 18/9/2015 17:55:57
myspecialpass - ******************* - 18/9/2015 17:56:2
imlearningpython - ******************* - 18/9/2015 17:56:15
imlearning - *************** - 18/9/2015 17:56:21
sillypass - ************** - 18/9/2015 17:56:29
lamepass - ************* - 18/9/2015 17:56:38
testcomplete - ****************** - 18/9/2015 17:56:56
test - ********* - 18/9/2015 17:57:0
testing - ************ - 18/9/2015 17:57:6
goodbye - ************ - 18/9/2015 17:57:9
bye - ******** - 18/9/2015 17:57:17
这真的很乱,我想清理干净。 首先..我可以在python中添加一些东西来制作一个标题为#34; Pass" "加密"和"约会"?其次,我想生成可以更好地格式化的代码。例如,最长的字符串之间要有五个空格。
这样的事情:
Password Encryption Date
hi ******* 18/9/2015 17:55:15
hello ********** 18/9/2015 17:55:20
test ********** 18/9/2015 17:55:23
testing ********** 18/9/2015 17:55:28
password ************* 18/9/2015 17:55:34
passwords ************** 18/9/2015 17:55:45
myspecialpassword ************************ 18/9/2015 17:55:57
myspecialpass ******************* 18/9/2015 17:56:2
imlearningpython ******************* 18/9/2015 17:56:15
imlearning *************** 18/9/2015 17:56:21
sillypass ************** 18/9/2015 17:56:29
lamepass ************* 18/9/2015 17:56:38
testcomplete ****************** 18/9/2015 17:56:56
test ********* 18/9/2015 17:57:0
testing ************ 18/9/2015 17:57:6
goodbye ************ 18/9/2015 17:57:9
bye ******** 18/9/2015 17:57:17
答案 0 :(得分:3)
您需要使用字符串对象的.format()方法。 E.g。
date = datetime.now().strftime('%m/%d/%y %H:%M:%S')
with open("capec_dictionary.txt", "a") as text_file:
template = '{:24s} {:24s} {:18s}\n'
text_file.write(template.format('Pass', 'Encryption', 'Date'))
writePass = finalPass.translate(passSwap)
text_file.write(template.format(finalPass, writePass, date))
答案 1 :(得分:2)
您正在寻找类似
的内容print('{:8s}'.format(my_str))
这会用右边的空格填充你的字符串,以确保它的宽度为8个字符。
对于标题,您可以将其作为固定字符串放在文件中。