Python 在文本文件中格式化多列数据
我现在正在学习python,所以我还是新手。我制作了一个加密系统,将初始密码、加密密码和日期输出到文本文件。目前的方法非常简单:Python 在文本文件中格式化多列数据,python,string-formatting,Python,String Formatting,我现在正在学习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_dictionar
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”的头吗?其次,我想生成可以更好格式化的代码。例如,最长的字符串应该是五个空格
大概是这样的:
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个字符
至于标题,您可以将其作为固定字符串放入文件中。您正在寻找类似的内容
print('{:8s}'.format(my_str))
这将在右边用空格填充字符串,以确保其宽度为8个字符
至于标题,您可以将其作为固定字符串放入文件。您需要使用string对象的.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))
您需要使用string对象的.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))
非常感谢。我能对标题(密码、加密、日期)做些什么吗?我也加了这个。。请参阅
打印(template.format('Pass','Encryption','Date'))
行很抱歉,我没有看到这个。不幸的是,使用此方法会给我一个错误“无效的格式字符串”,否则:date=datetime.now().strftime(“%D%T”)将open(“capec_dictionary.txt”,“a”)作为文本文件:template='{:24s}{:24s}{:18s}打印(template.format('Pass','Encryption','date'))writePass=finalPass.translate(passSwap)print(template.format(finalPass,writePass,date))print(“密码已保存到字典中”)抱歉..我不知道这里看起来会很混乱。是的,我相信我键入的是正确的。谢谢。我能对标题(密码,加密,日期)做些什么吗?我还添加了这个..参见打印(template.format('Pass','Encryption','Date'))
line很抱歉,我没有看到这一点。不幸的是,使用此方法会给我一个错误“格式字符串无效”else:Date=datetime.now().strftime('%D%T'),打开(“capec_dictionary.txt”,“a”)作为文本文件:template='{:24s}{:24s}{:18s}打印(template.format('Pass','Encryption','Date'))writePass=finalPass.translate(passSwap)print(template.format(finalPass,writePass,Date))print(“密码已保存到字典中”)抱歉..不知道这里看起来会很混乱。是的,我相信我键入的是正确的。