Japanese output in Python

Introduction

I thought I'd start python, well, after all

print("Hello world")

Yeah, good.

print ("Japanese")

Traceback (most recent call last):
  File "/tmp/a.py", line 1, in <module>
    print("\u65e5\u672c\u8a9e");
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)

:cry: :-1:

Ordinary solution

After a lot of research, I found that I had to convert between unicode and byte.

print ("Japanese" .encode ("utf-8"))

b'\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e'

No, that's not the case.

setenv LANG ja_JP.UTF-8
Japanese

Well, it worked. But there are more problems!

I want to start from cron

When you start cron, the parent process is cron, so naturally it inherits the cron environment. That is the story of what kind of locale is set for the entire system. However, since the policy of the entire system is unified with basic C (when ls or date is set to the Japanese locale format, various things do not work), the cron environment is naturally LANG = C. It can't be changed.

Digression: shebang

To start the script, write #! / usr / local / bin / python3 on the first line. There are many ways to call this, but it seems to be shebang. So, the one called by this cannot use shebang twice. In other words, when #! / usr / local / bin / foo, the first line of / usr / local / bin / foo should not be #! / usr / local / bin / bar. .. I did not know that. Perhaps you are calling an exec () series of functions in a system call.

I want to set environment variables and start

The idea of making / usr / local / bin / python3 a shell script was frustrated, so I wrote it in C.

python3.c


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char **argv){
  putenv("LANG=ja_JP.UTF-8");
  argv[0] = "/usr/local/bin/python3.5";
  argv[argc] = NULL;
  execv("/usr/local/bin/python3.5",argv);
}

Now it works only with the first print ("Japanese "). (I don't need encode anymore)

python3 is no longer a symbolic link, but it's a workaround, so please be patient.

Somehow solved. The road to Pythonista is steep

Recommended Posts

Japanese output in Python
I wrote python in Japanese
I understand Python in Japanese!
Read Fortran output in python
Get Japanese synonyms in Python
Japanese output when dealing with python in visual studio
Output 2017 Premium Friday list in Python
Make standard output non-blocking in Python
Export and output files in Python
How to handle Japanese in Python
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
python learning output
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
Data input / output in Python (CSV, JSON)
Output tree structure of files in Python
Sorted list in Python
Clustering text in Python
Daily AtCoder # 2 in Python
Implement Enigma in python
Daily AtCoder # 6 in Python
Daily AtCoder # 18 in Python
Edit fonts in Python