Search code examples
pythontextencodingcommand-line

Python command line input doesn't delete japanese characters when pressing the erase key


I have a command line python application which reads japanese characters from the command line input. The problem is that the 'erase' key doesn't work very well, let me explain:

What happens is that, when I try to delete a kanji, the character seems to not be deleted: the character is actually deleted (I know this because I have debugged it) but it still appears in the command line. E.g., suppose I have the following characters:

四つください。

Now, if I press the erase key 3 times I get:

四つくださ

As you can see, only two characters have been deleted (the and the ). However, if I press enter, the script interprets correctly that the only remaining characters are 四つくだ. So in other words, the 3 characters are actually deleted, but the is still visible in the terminal.

What can I do to solve this?


Solution

  • The problem gets fixed doing the following:

    # In the terminal:
    pip install readline
    
    
    # In the script
    import readline 
    

    Don't know why that fixes the problem, but it does.