0

I'm using Win10 & Scite with utf-8 enabled output window. The file is saved as UTF-8 with BOM

Script:

print('ダイスキ from python 3')

The script can be run on cmd prompt without error. But when run on Scite it will produce error:

Output:

>pythonw.exe -u "test.py"
Traceback (most recent call last):
  File "test.py", line 12, in <module>
    print('\u30c0\u30a4\u30b9\u30ad from python 3')
  File "D:\BIN\Python37\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 1-2: character maps to <undefined>
>Exit code: 1

How to properly print ダイスキ to stdout using python3 with Scite?


Updates:

I've edited Scite Global Options file, to support utf-8.

code.page=65001

I've tested C, Lua, old Python 2.7, and it can print utf-8 strings (on Scite output window).

Seems to be Scite configuration error or a maybe Scite bug, because the Scite output terminal window works on Lua & C, but fail only on Python3.

the script

dns
  • 2,753
  • 1
  • 26
  • 33
  • Why all the bending over backwards with `encode` and `decode`? What about a plain `print('ダイスキ')`? – deceze Dec 14 '18 at 15:27
  • Well, I think it's terminal output issue. The script can be run on `cmd prompt` but with strange text & no error. But if it run on `Scite`, it will produce error. It's definetely a `Scite` bug. It works on `C & Lua`, but fail with `Python3`. – dns Dec 14 '18 at 16:26
  • What’s the error if you skip that `encode` `decode` dance? – deceze Dec 14 '18 at 16:41
  • It's same error. `Traceback (most recent call last): File "test-requests.py", line 16, in print("\u30c0\u30a4\u30b9\u30ad") File "D:\BIN\Python37\lib\encodings\cp1252.py", line 19, in encode return codecs.charmap_encode(input,self.errors,encoding_table)[0] UnicodeEncodeError: 'charmap' codec can't encode characters in position 0-3: character maps to ` – dns Dec 14 '18 at 16:43
  • So simplify the question by skipping `encode` `decode`, because it likely has nothing to do with the problem. The problem is that UTF-8 output doesn’t work in your particular environment, let’s focus on that environment. – deceze Dec 14 '18 at 16:46
  • Edited your question, please ensure it’s still correct. – deceze Dec 14 '18 at 16:52
  • I added scite config to the question. – dns Dec 14 '18 at 17:05
  • SciTE thinks the stdout encoding is `cp1252`. Are you sure you are configuring SciTE correctly for UTF-8? – Mark Tolonen Dec 14 '18 at 17:53
  • @MarkTolonen: I already changed to `code.page=65001` but still receive the same error message. I can print other unicode character except Japan/Chinese character. – dns Dec 15 '18 at 01:16
  • The error message still says `encodings\cp1252.py`, so it thinks that's the stdout encoding. – Mark Tolonen Dec 15 '18 at 02:32
  • According to https://www.scintilla.org/SciTEDoc.html there is an `output.code.page` parameter as well. Is that set and overriding `code.page`? – Mark Tolonen Dec 15 '18 at 02:39
  • I set `output.code.page` but still return the same error. I just installed old Python 2.7 and it works fine with Scite & Unicode. Only Python 3.7 (C/J/K/Arabic character) doesn't work with Scite. It's very strange. – dns Dec 15 '18 at 03:36

2 Answers2

1

Scite involves popen() / piping the STDOUT.

Python 3.7 need env variable "PYTHONIOENCODING" to be set manually. So you need to add environment variable "PYTHONIOENCODING" set to "utf_8"

enter image description here

Result:

enter image description here

dns
  • 2,753
  • 1
  • 26
  • 33
-3

Try to do this:

print(u'ダイスキ')