codecs 是处理编码的模块,对多国语言的处理有良好的支持,
小例子,
# python2.7
look = codecs.lookup( " utf-8 " )
a = " 我爱北京 "
b = look.decode(a) # 把a编码为utf-8
print(b)
b2 = look.encode(b[0]) # 将b转换回str
b3 = look.encode(u'\u90fd\u6325\u6d12\u7684\u5ba2\u6237') # 也可以将utf-8字符转换为str
bfile = codecs.open( " dddd.txt " , ' r ' , " utf-8" ) #指定编码方式打开文件
网友评论