Note on encoding when LANG = C in Python

When the LANG of the environment that runs a certain Python script changes from ja_JP.UTF-8 to C, an error comes out, so make a note of the countermeasures around that.

#!/usr/bin/python3
# -*- coding: utf8 -*-

import sys

from logging import getLogger, StreamHandler, DEBUG

handler = StreamHandler()

logger = getLogger(__name__)
logger.setLevel(DEBUG)
logger.addHandler(handler)


str = "Hoge"
tf = open('utf8.txt')
s = tf.read()

print(str)
print(s)

logger.debug(str)
logger.debug(s)

It's usually fine to run this with LANG = ja_JP.UTF-8. This is usually because Python is set to look at LANG at startup and change the default I / O encoding. So if you bring this to LANG = C, it will try to interpret UTF-8 as ASCII and give an exception.

First, specify the encoding to read the file.

tf = open('utf8.txt', encoding='utf8')

Specify that the output destination of sys.stdout used for print is UTF-8.

sys.stdout = codecs.getwriter("utf8")(sys.stdout.detach())
sys.stderr = codecs.getwriter("utf8")(sys.stderr.detach())

The logger works as it is, but the output is escaped, so use the modified sys.stderr.

handler = StreamHandler(sys.stderr)

In summary, it looks like this.

#!/usr/bin/python3
# -*- coding: utf8 -*-

import codecs
import locale
import sys


from logging import getLogger, StreamHandler, DEBUG, Formatter

sys.stdout = codecs.getwriter("utf8")(sys.stdout.detach())
sys.stderr = codecs.getwriter("utf8")(sys.stderr.detach())

handler = StreamHandler(sys.stdout)

logger = getLogger(__name__)
logger.setLevel(DEBUG)
logger.addHandler(handler)



str = "Hoge"
tf = open('utf8.txt', encoding='utf8')
s = tf.read()

print(str)
print(s)

logger.debug(str)
logger.debug(s)

reference

Recommended Posts

Note on encoding when LANG = C in Python
Run Python in C ++ on Visual Studio 2017
Next Python in C
Character encoding when dealing with files in Python 3
Encoding judgment in Python
C API in Python 3
Note when putting lxml of python package in ubuntu 14.04
Things to note when initializing a list in Python
A note on handling variables in Python recursive functions
Extend python in C ++ (Boost.NumPy)
Base64 encoding images in Python 3
Python Input Note in AtCoder
Binary search in Python / C ++
Attention when os.mkdir in Python
A note on touching Microsoft's face recognition API in Python
Precautions when using pit in Python
[Note] Project Euler in Python (Problem 1-22)
Behavior when listing in Python heapq
Support when installing pillow on python3.9
When reading C ++ structs in Cython
Solve ABC036 A ~ C in Python
How to wrap C in Python
Foreign Key in Python SQLite [Note]
Solve ABC037 A ~ C in Python
Write C unit tests in Python
When using regular expressions in Python
When writing a program in Python
Call C / C ++ from Python on Mac
Resolved an error when putting pygame in python3 on raspberry pi
Things to note when running Python on EC2 from AWS Lambda
A note on using tab completion when running Python interactively on Windows
When specifying multiple keys in python sort
python note: when easy_install is not available
Precautions when pickling a function in python
Find files like find on linux in Python
Notes on nfc.ContactlessFrontend () for nfcpy in python
Algorithm in Python (ABC 146 C Binary Search
Note when creating an environment with python
Implement FIR filters in Python and C
To set default encoding to utf-8 in python
[Python] Calculate the number of digits required when filling in 0s [Note]
[C] [python] Read with AquesTalk on Linux
Run AzureKinect in Python on Christmas Eve.
Write O_SYNC file in C and Python
Note: Python
When looking at memory usage in Python 3
Python encoding
Python note
Generate C language from S-expressions in Python
A useful note when using Python for the first time in a while
Notes on using code formatter in Python
I stumbled on the character code when converting CSV to JSON in Python
[Tips] Easy-to-read writing when connecting functions in Python
When codec can't decode byte appears in python
How to use the C library in Python
Output formatted output in Python, such as C / C ++ printf.
3 ways to parse time strings in python [Note]
[Note] Python environment construction on rental server "CORESERVER"
How to generate permutations in Python and C ++
Install python package in personal environment on Ubuntu
When I try matplotlib in Python, it says'cairo.Context'