0

I am trying to add timestamp to writelines() so that I get the timestamp in between the bullet point and the hyphen in the code below:

from datetime import datetime
datetime.now()
A = ['ABC', 'PQR', 'XYZ']
f = open("test.dat", 'w')
f.writelines(list(u'\u27B3 - %s\n'.encode('utf-8') % i for i in A))
f.close()

Output

➳ - ABC
➳ - PQR
➳ - XYZ

Desired output

➳ 18:15:02 - ABC
➳ 18:15:02 - PQR
➳ 18:15:02 - XYZ

1 Answer 1

3

You could import datetime module and concatenate the strings in the appropriate format. Using your own code:

from datetime import datetime
datetime.now()
A = ['ABC', 'PQR', 'XYZ']
f = open("test.dat", 'w')
f.writelines(list(u'\u27B3 '.encode('utf-8') + str(datetime.now().strftime("%H:%M:%S")) + ' - %s\n' % i for i in A))
f.close()
Sign up to request clarification or add additional context in comments.

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.