encode and decode, Chinese garbled

https://blog.csdn.net/Y601343678/article/details/78585728 p> 

How to get the default coding system?

#!/usr/bin/env python

#coding=utf-8

import sys

printsys.getdefaultencoding ()

The program output section is in English WindowsXP: ascii

The program output section is in English Windows7: mbcs

UliPad run in the following code:

s = u "Chinese"

print s

会提示:UnicodeEncodeError:'ascii' codec can't encode characters in position 0-1: ordinal notinrange(128)。

This is because in the English UliPad WindowsXP Console window in accordance with the information output ascii encoded output (the English system is the default encoding ascii),

The above code is a string of Unicode, an error output is generated.

The last sentence: prints.encode ( 'gb2312')

It is possible to output a "Chinese" word.

If the last sentence: prints.encode ( 'utf8')

Output: \ xe4 \ xb8 \ xad \ xe6 \ x96 \ x87,

This is the output window or console output string coded according utf8 ascii code.

unicode (str, 'gb2312') and str.decode ( 'gb2312') is the same, both will gb2312 encoded into unicode encoding str

Use str .__ class__ you can see the coded form of str

Guess you like

Origin www.cnblogs.com/heluobing/p/11229255.html