Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set up Notepad++ and NppExec to print unicode characters from python

I have an utf-8 encoded file cjk.py:

print("打印")

Unsurprisingly, running python cjk.py yields

Traceback (most recent call last):
  File "cjk.py", line 1, in <module>
    print('\u6253\u5370')
  File "C:\Python33\lib\encodings\cp850.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_map)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 0-1: character maps to <undefined>

Yet running idle -r cjk.py works perfectly:

打印

Can I configure notepad++'s NppExec plugin to behave like Idle? I've trying setting input and output encoding to UTF-8, to no avail (same exception as when running python cjk.py from the console)

like image 240
Clément Avatar asked Aug 23 '13 13:08

Clément


People also ask

How do I print Unicode characters in Python?

To include Unicode characters in your Python source code, you can use Unicode escape characters in the form \u0123 in your string. In Python 2. x, you also need to prefix the string literal with 'u'.

How do you use Unicode characters in Python?

In Python, the built-in functions chr() and ord() are used to convert between Unicode code points and characters. A character can also be represented by writing a hexadecimal Unicode code point with \x , \u , or \U in a string literal.

How do you create a Unicode string in Python?

You have two options to create Unicode string in Python. Either use decode() , or create a new Unicode string with UTF-8 encoding by unicode(). The unicode() method is unicode(string[, encoding, errors]) , its arguments should be 8-bit strings.

Does Python support Unicode?

Python's string type uses the Unicode Standard for representing characters, which lets Python programs work with all these different possible characters.


1 Answers

I had the same problem and fixed it.

Add env_set PYTHONIOENCODING=utf-8 just below C:\Python27\python.exe "$(FULL_CURRENT_PATH)" in the dialog box when you press F6.

Worked like a charm for me, hope it helps.

Source: http://sourceforge.net/p/npp-plugins/discussion/672146/thread/d94ff609/

like image 63
HedonNNN Avatar answered Nov 15 '22 14:11

HedonNNN