# # str --> utf-8
s1 = 'www中国'
b1 = s1.encode('utf-8')
print(b1)
# str --> gbk
s1 = 'www中国'
b1 =s1.encode('gbk')
print(b1)
# utf-8 -->bytes
b3 = b'www\xe4\xb8\xad\xe5\x9b\xbd'
s = b3.decode('utf-8')
print(s) #www中国
#gbk -->bytes
b4 = s.encode('gbk')
print(b4) #b'www\xd6\xd0\xb9\xfa'
f = open(r"D:\a.txt",'r',encoding = 'utf-8')
print(f.read())
f.close()
f1 = open('r模式',encoding = 'utf-8')
for line in f1:
print(line)
f1.close()
with open('timg.jpg',mode = 'rb') as f3:
content=f3.read()
with open('timg2.jpg',mode = 'wb') as f4:
f4.write(content)
f3.close()
f4.close()
转载于:https://www.cnblogs.com/andyyangpython/p/10495388.html