Skip to content Skip to sidebar Skip to footer

Unicode Symbols In Output File In Python 3.6.1

I need to log Connection errors to log.txt. Windows is Russian. My code: # e is a name for 'requests.ConnectionError' form Windows if server is not avilable # I take error

Solution 1:

First: what is codec codecs.open('log.txt', 'a', encoding='utf-8')?

Second: this is not right strftime(str("%H:%M:%S %Y-%m-%d") + str(e_str_unicode) + '\n') it should be strftime("%H:%M:%S %Y-%m-%d") + e_str_unicode + '\n'

This is a short example how to do it:

from time import strftime
text = input()
print(text)

with open('log.text', 'a', encoding='utf-8') as log:
    message = strftime("%H:%M:%S %Y-%m-%d") + '=>' + text + '\n'
    log.write(message)

Post a Comment for "Unicode Symbols In Output File In Python 3.6.1"