-
如何知道系统默认的编码
-
import sys
-
sys.getdefaultencoding()
output:
'utf-8'
-
设置系统默认编码
import sys
-
reload(sys)
-
sys.setdefaultencoding('utf8')
-
解码 然后编码
print( " china".decode('utf-8').encode('gbk') ) ##python3 使用 unicode
output: china
print( u' china') #python3 使用 unicode
output: china
##################################################################
- str and Unicode
str(byte flow)
Unicode(string flow)
- ASCII(1 byte(7bits+1bit sign) represents 1char, most 127 chars)
- à ISO-8859-1(8bits,most 256 chars) à
- (GB: Chinese) GB2312 àGBK à GB18030
- Unicode(ucs-2, ucs-4) (cost space + time)
- UTF-8(efficiency) à UTF-32
Process Chinese string: GBK->Unicode->UTF-8
Decode: GBKàUnicode
Encode: UnicodeàGBK
###################################################################
AttributeError: 'str' object has no attribute 'decode'
OR
b'\xc7\xeb\xca\xe4\xc8\xeb\xc3\xfb\xd7\xd6: '
Solution:
*****************************************************************
#example 1 & example 2
import sys
print(sys.stdin.encoding) #Current: utf-8
print(sys.stdout.encoding) #Current: utf-8
x = input(u"请输入名字: ".encode(sys.stdout.encoding).decode(sys.stdout.encoding))# encodeError solution: ".encode('gbk') "
f = open('1.txt', 'w')
#Current
f.write(x.encode(sys.stdin.encoding).decode(sys.stdin.encoding))
f.close()
- 查询系统路径
-
import sys
-
sys.path