neolei
neolei

Reputation: 1948

VSCode - Output window says "UnicodeEncodeError" when I try to print unicode

I just started to use VSCode on some python scripts. When I try to print something like:

print('%s' % string.decode('utf-8'))

I get following error in output window:

UnicodeEncodeError: 'ascii' codec can't encode characters in position 16-20: ordinal not in range(128)

I know it probably because the output stream is ascii only, but I can't find a place to change it... , either I missed it, or it just the way it is?

I'm using v0.10.8 on Win7 64bit.

Thanks in advance!

Upvotes: 3

Views: 5438

Answers (2)

patiencing
patiencing

Reputation: 106

I suffer the same problem, it's caused by the the plugin -- Code Runner.

Please check the discussions here:

It suggest to write these into VSCode setting file:

{
    "code-runner.executorMap": {
        "python": "set PYTHONIOENCODING=utf8 && python"
    }
}

if, it doesn't work, you can use this (run the script in terminal):

{
    "code-runner.runInTerminal": true
}

Upvotes: 6

Tim Seed
Tim Seed

Reputation: 5279

Your syntax is correct

arabic_bytes=b'\xd9\x83\xd9\x84\xd8\xa7 \xd8\xa8\xd8\xaf\xd8\xa7\xd9\x8a\xd8\xa9 \xd9\x88\xd8\xa8\xd8\xaf\xd8\xa7\xd9\x8a\xd8\xa9'

print('%s' % arabic_bytes.decode('utf-8'))

And you should see كلا بداية وبداية

It is possible that your Byte string is corrupted in which case change you decode to be

 arabic_bytes.decode('utf-8', errors='ignore')

Upvotes: 0

Related Questions