If you are using Mac, add the following code block to the beginning of the file (the first line of the file).

#!/usr/bin/python
# -*- coding: utf-8 -*-

It will force the interpreter to use UTF-8 encoding method.

If you are using Windows, it does not work.

There are just two examples in Windows,

datetime.datetime.now().strftime('%Y{0}%m{1}%d{2}').format(*'年月日')
open('text1.txt', "w" , encoding='utf8')
  • encoding argument will force Python write text files in UTF-8 format.