How to enter Japanese with Python curses

environment

Introduction

When I used getch () of curses, I couldn't receive Japanese (multibyte characters) well when encoding with UTF-8, so I made a processing part.

Symptoms

For example, if you type'a', it will be divided into 3 bytes of 0xe3 0x81 0x82 and will be input 3 times. However, this is inconvenient. (What I really want is 0x3042)

Cause

Since getch () looks at the input byte by byte, UTF-8 cannot correctly receive Japanese etc. which is 3 bytes. So, I will make my own UTF-8 encoder.

solution

Looking at the first byte, the number of bytes in the character string is fixed, so conditional branching is performed as follows.

import curses

window = curses.stdscr()
key = window.getch()

#Processing of multi-byte characters
#In Japanese, it's 3 bytes, so you need to pool it. Look at the first byte
#Since the remaining vices and the number are fixed, the process is performed.
text_pool = [key]
if 0x00 <= key <= 0x7f:
     #1B so you don't have to do anything
     #ascii compatible area
     pass
elif 0x80 <= key <= 0xbf:
     #This should be after the second character, so it would be strange if it came in
     print(key)
     exit(1)
elif 0xc0 <= key <= 0xdf:
     #2B characters with umlauts
     text_pool.append(self.window.getch())
     # text_pool => [0dAAA, 0dBBB]
     # 110a aabb 10bb bbbb <=This is text_Contents of pool(Decimal version)
     #0b00000aaa bbbbbbbb is taken out and char c= (char) (data[i] & 0xff);
     #Convert to decimal number and assign to key
     a, b = text_pool
     tmp = map(lambda x: bin(x)[2:], [0b00011111 & a, 0b00111111 & b])
     tmp = ''.join(item.zfill(6) for item in tmp)
     key = int(tmp,2)
elif 0xe0 <= key <= 0xef:
     #3B Japanese is here
     for _ in range(2):
         text_pool.append(self.window.getch())
         a, b, c = text_pool
         # 0b 1110xxxx 10xxyyyy 10yyyyyy
         # 0d a        b        c
         tmp = map(lambda x: bin(x)[2:], [0b00001111 & a, 0b00111111 & b, 0b00111111 & c])
         tmp = ''.join([item.zfill(6) for item in tmp])
         key = int(tmp,2)
elif 0xf0 <= key <= 0xff:
#4B I've never seen it, but bug fixing
     for _ in range(3):
         text_pool.append(self.window.getch())
         a, b, c ,d = text_pool
         # 11110xxx 10xxyyyy 10yyyyzz 10zzzzzz
         tmp = map(lambda x: bin(x)[2:], [0b00000111 & a, 0b00111111 & b, 0b00111111 & c, 0b00111111 & d])
         tmp = ''.join([item.zfill(6) for item in tmp])
         key = int(tmp,2)
else:
    #Special key
    pass


print(chr(key))

Relation

About mouse wheel judgment https://qiita.com/t4t5u0/items/ae6d25e05b7f7094330e

reference

https://seiai.ed.jp/sys/text/cs/mcodes/ucodeutf8.html

Recommended Posts

How to enter Japanese with Python curses
How to display python Japanese with lolipop
[Python] How to handle Japanese characters with openCV
How to get mouse wheel verdict with Python curses
How to get started with Python
How to use FTP with Python
How to calculate date with python
How to handle Japanese in Python
How to work with BigQuery in Python
How to use Japanese with NLTK plot
[Python] How to deal with module errors
How to install python3 with docker centos
How to not escape Japanese when dealing with json in python
How to install Python
How to upload with Heroku, Flask, Python, Git (4)
How to enjoy programming with Minecraft (Ruby, Python)
[REAPER] How to play with Reascript in Python
How to do multi-core parallel processing with python
How to crop an image with Python + OpenCV
How to measure execution time with Python Part 1
How to use tkinter with python in pyenv
How to make Linux compatible with Japanese keyboard
[Python] How to compare datetime with timezone added
How to measure execution time with Python Part 2
Connect to BigQuery with Python
How to convert / restore a string with [] in python
Send Japanese email with Python3
How to install Python [Windows]
python3: How to use bottle (2)
How to scrape image data from flickr with python
How to do hash calculation with salt in Python
[Introduction to Python] How to iterate with the range function?
Connect to Wikipedia with Python
How to update Python Tkinter to 8.6
Post to slack with Python 3
How to upload with Heroku, Flask, Python, Git (Part 3)
How to run tests in bulk with Python unittest
[Python] How to specify the download location with youtube-dl
How to measure mp3 file playback time with python
How to use python interactive mode with git bash
How to use Python argparse
How to update with SQLAlchemy?
It's too troublesome to display Japanese with Vim's python3.
How to cast with Theano
How to upload with Heroku, Flask, Python, Git (Part 1)
[Python] How to deal with pandas read_html read error
[Python] How to use checkio
Japanese morphological analysis with Python
How to run Notepad ++ Python
How to upload with Heroku, Flask, Python, Git (Part 2)
How to Alter with SQLAlchemy?
Switch python to 2.7 with alternatives
Write to csv with Python
How to separate strings with','
How to change Python version
[Python] How to rewrite the table style with python-pptx [python-pptx]
How to RDP with Fedora31
[Python] How to create a 2D histogram with Matplotlib
How to develop in Python
[python] How to judge scalar
[Python] How to use input ()