格式化文本文件中的多列数据
Formatting multiple columns of data in a text file
我现在正在学习 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 中附加一些东西来制作 "Pass" "Encryption" 和 "date" 的 header 吗?其次,我想生成可以更好地格式化的代码。比如最长的字符串要在5个空格之间。
像这样:
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
您正在寻找类似
的东西
print('{:8s}'.format(my_str))
这会在您的字符串右侧填充空格以确保其宽度为 8 个字符。
至于header,您可以将其作为固定字符串放入文件中。
您需要使用字符串对象的.format()方法。例如
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))
我现在正在学习 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 中附加一些东西来制作 "Pass" "Encryption" 和 "date" 的 header 吗?其次,我想生成可以更好地格式化的代码。比如最长的字符串要在5个空格之间。
像这样:
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
您正在寻找类似
的东西print('{:8s}'.format(my_str))
这会在您的字符串右侧填充空格以确保其宽度为 8 个字符。
至于header,您可以将其作为固定字符串放入文件中。
您需要使用字符串对象的.format()方法。例如
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))