0

I am learning python as of right now so I am still new. I have made an encryption system that outputs the initial password, encrypted password, and date to a text file. The method right now is very simple:

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.")

The result is as shown:

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

This is really messy and I want to clean in up. First.. can I append something in python to make a header of "Pass" "Encryption" and "date"? Secondly I want to generate code in which it can be better formatted. For example, the longest string to to be five spaces between.

Something like this:

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

2 Answers 2

3

You need to use the .format() method of the string object. 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))
Sign up to request clarification or add additional context in comments.

7 Comments

Thank you. Is there anything I can do about the header (Password, Encrypt, Date)?
I added that also.. See the print(template.format('Pass', 'Encryption', 'Date')) line
I'm sorry I did not see this. Unfortunatly using this method gives me an error "invalid format string"
else: date = datetime.now().strftime('%D %T') with open("capec_dictionary.txt", "a") as text_file: template = '{:24s} {:24s} {:18s}' print(template.format('Pass', 'Encryption', 'Date')) writePass = finalPass.translate(passSwap) print(template.format(finalPass, writePass, date)) print("Password has been saved to dictionary.")
Sorry.. didn't know it would look that messed up on here. Yes I believe I typed it correctly.
|
2

You're looking for something like

print('{:8s}'.format(my_str))

This will pad your string on the right with spaces to ensure it's 8 characters wide.

As for the header, you can just put that in the file as a fixed string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.