My Tech Life

Memo by a Japanese Software Developer in his late 50s.

english post: 'cp932' codec can't encode character '\x##' in position ####: illegal multibyte sequence

'cp932' codec can't encode character '©' in position 3337: illegal multibyte sequence

 

Exception commonly encountered

when running Python on Japanese version of Windows,

but almost never observed on Linux.

 

Verify the point below

before delving into the intricacies of encoding and decoding of python.

 

And note to myself.

 

At the line corresponding to the stack trace,

typical print line is written like this.

print(some_text)

 

When running the script from the command line and outputting directly,

it does not raise an exception.

python some_script.py

 

However, redirecting the output results in raising an exception.

python some_script.py > result.txt

 

Avoiding the convenient redirection

and opting for proper file writing with specified encoding

improves the situation,

but it may be more cumbersome.

with open(outputfilename, 'w', encoding='utf-8') as fout:

 

In essence, it seems that

the cause of the exception during redirection in command-line execution

was the redirect's inability to specify encoding.