乱码的原因
字符A,ASCII编码:01000001, Unicode编码00000000 01000001
字符A,ASCII编码:01000001, UTF-8编码01000001
一般读写
一般读取:文件(uncode)到电脑需要转为(utf-8)
一般写入:电脑(uncode)到文件需要转为(uncode)
Python3 字符串默认使用unicode编码, 所以他支持国际化
以unicode表示的str通过encode()方法编码为指定的bytes
print(html.read().decode('utf-8'))
UTF-8
Python 2.x 的文件第一行增加代码# - codeing:utf8 -,
解释器就以utf-8处理文件
也可以使用# coding=utf8
Python 2.x 遍历字符串乱码
# *-* coding:utf8 *-*
# 引号前面的u 告诉解释器这是一个utf8编码的字符串
str = u"hello 世界"
print(str)
for s in str:
prrint(s)